Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import discord
- from discord.commands import Option
- from discord.interactions import Interaction
- from discord.ui import Button, View
- #to join paths
- import os
- bot = discord.Bot()
- #discord bot token
- token = os.environ.get('TOKEN')
- #token of my test server
- guildID = [insert guild id]
- platforms = ['Bloggity', 'Flitter', 'XPosure']
- #makes post buttons
- class postEngagement(View):
- def __init__(self, platform, handle, makepost):
- super().__init__()
- self.platform = platform
- self.handle = handle
- self.makepost = makepost
- #A retweet/share with comment
- @discord.ui.button(label="share", style=discord.ButtonStyle.green, emoji="🔁")
- async def share (self, button: Button, interaction: Interaction):
- await interaction.response.defer()
- # checks to make sure the comment only occurs for the user who interacted with the button
- def check(m):
- if m.author == interaction.user:
- return m
- comment = await bot.wait_for(event='message', check=check)
- if comment:
- await comment.delete()
- shareComment = f"> {self.makepost}\n\n ***@{self.handle}*** {comment.content}"
- await post(self.platform, interaction.user.display_name, interaction.message, shareComment, None)
- # formats the posts according to each channel 'platform'
- def platform_post(platform=str, handle=str, makepost=str):
- # character limit for flitter
- if platform == "Flitter":
- if len(makepost) >= 280:
- toolong = len(makepost) - 280
- error = f"Your Flitter post was {toolong} characters too long!\n\n_{makepost}_\n\nFlit shorter!"
- return(error)
- else:
- embed=discord.Embed(description=makepost, title="@" + handle, colour=0x55acee)
- return(embed)
- elif platform == "XPosure":
- embed=discord.Embed(description=makepost, title="@" + handle, colour=0xE1306C)
- return(embed)
- elif platform == "Bloggity":
- embed=discord.Embed(description=makepost, title="@" + handle, colour=0xffea00)
- return(embed)
- # makes a post for the bot
- async def post(platform, handle, msg, makepost, thread=None):
- content = platform_post(platform, handle, makepost)
- view = postEngagement(platform, handle, makepost)
- webhooks = await msg.guild.webhooks()
- #gets specific webhook that matches the platform name
- webhook = discord.utils.get(webhooks, name=platform)
- #if the content of the post is an embed and there's a view (ie reaction buttons), send message
- if isinstance(content, discord.Embed):
- if thread:
- await webhook.send(embed=content, thread=thread)
- else:
- await webhook.send(embed=content, view=view)
- # if the post is not an embed (which implies there's an 'error'), return the error
- else:
- # if the ctx is NOT an Interaction (ie is not from postEngagement buttons)
- if not isinstance(content, Interaction):
- await msg.reply(content, ephemeral=True)
- # if it is from postEngagement buttons
- else:
- await msg.reply(content, ephemeral=True)
- @bot.event
- async def on_message(message):
- if message.author.bot:
- return
- if message.channel.name.capitalize() in platforms:
- platform = message.channel.name.capitalize()
- makepost = message.content
- handle = message.author.display_name
- await post(platform, handle, message, makepost)
- bot.run(token)
Advertisement
Add Comment
Please, Sign In to add comment