Guest User

Untitled

a guest
Jan 13th, 2022
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.55 KB | None | 0 0
  1. import discord
  2. from discord.commands import Option
  3. from discord.interactions import Interaction
  4. from discord.ui import Button, View
  5.  
  6. #to join paths
  7. import os
  8.  
  9. bot = discord.Bot()
  10.  
  11. #discord bot token
  12. token = os.environ.get('TOKEN')
  13.  
  14. #token of my test server
  15. guildID = [insert guild id]
  16.  
  17. platforms = ['Bloggity', 'Flitter', 'XPosure']
  18.  
  19.  
  20. #makes post buttons
  21. class postEngagement(View):
  22.     def __init__(self, platform, handle, makepost):
  23.         super().__init__()
  24.         self.platform = platform
  25.         self.handle = handle
  26.         self.makepost = makepost
  27.  
  28.     #A retweet/share with comment
  29.     @discord.ui.button(label="share", style=discord.ButtonStyle.green, emoji="🔁")
  30.     async def share (self, button: Button, interaction: Interaction):
  31.  
  32.         await interaction.response.defer()
  33.  
  34.     # checks to make sure the comment only occurs for the user who interacted with the button
  35.         def check(m):
  36.             if m.author == interaction.user:
  37.                 return m
  38.  
  39.         comment = await bot.wait_for(event='message', check=check)
  40.  
  41.         if comment:
  42.             await comment.delete()
  43.             shareComment = f"> {self.makepost}\n\n ***@{self.handle}*** {comment.content}"
  44.             await post(self.platform, interaction.user.display_name, interaction.message, shareComment, None)
  45.  
  46. # formats the posts according to each channel 'platform'
  47. def platform_post(platform=str, handle=str, makepost=str):
  48.     # character limit for flitter
  49.     if platform == "Flitter":
  50.         if len(makepost) >= 280:
  51.             toolong = len(makepost) - 280
  52.             error = f"Your Flitter post was {toolong} characters too long!\n\n_{makepost}_\n\nFlit shorter!"
  53.             return(error)
  54.         else:
  55.             embed=discord.Embed(description=makepost, title="@" + handle, colour=0x55acee)
  56.             return(embed)
  57.     elif platform == "XPosure":
  58.         embed=discord.Embed(description=makepost, title="@" + handle, colour=0xE1306C)
  59.         return(embed)
  60.     elif platform == "Bloggity":
  61.         embed=discord.Embed(description=makepost, title="@" + handle, colour=0xffea00)
  62.         return(embed)
  63.  
  64. # makes a post for the bot
  65. async def post(platform, handle, msg, makepost, thread=None):
  66.  
  67.     content = platform_post(platform, handle, makepost)
  68.  
  69.     view = postEngagement(platform, handle, makepost)
  70.            
  71.     webhooks = await msg.guild.webhooks()
  72.     #gets specific webhook that matches the platform name
  73.     webhook = discord.utils.get(webhooks, name=platform)
  74.     #if the content of the post is an embed and there's a view (ie reaction buttons), send message
  75.     if isinstance(content, discord.Embed):
  76.         if thread:
  77.             await webhook.send(embed=content, thread=thread)
  78.         else:
  79.             await webhook.send(embed=content, view=view)
  80.         # if the post is not an embed (which implies there's an 'error'), return the error
  81.     else:
  82.         # if the ctx is NOT an Interaction (ie is not from postEngagement buttons)
  83.         if not isinstance(content, Interaction):
  84.             await msg.reply(content, ephemeral=True)
  85.         # if it is from postEngagement buttons
  86.         else:
  87.             await msg.reply(content, ephemeral=True)
  88.            
  89.  
  90. @bot.event
  91. async def on_message(message):
  92.     if message.author.bot:
  93.         return
  94.  
  95.     if message.channel.name.capitalize() in platforms:
  96.         platform = message.channel.name.capitalize()
  97.         makepost = message.content
  98.         handle = message.author.display_name
  99.         await post(platform, handle, message, makepost)
  100.  
  101.  
  102. bot.run(token)
Advertisement
Add Comment
Please, Sign In to add comment