Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import discord
- import random
- from discord.ext import commands
- bot = commands.Bot(command_prefix="!")
- bot.remove_command("help")
- def keys_to_int(x):
- """
- Json doesn't allow keys as integers.
- When saving we will have
- {123: 567}
- where 123 is anchored channel ID and 567 anchored message ID
- both are int, and we need to load the as int too.
- But json forces keys as strings upon saving so we need to convert them back to int
- """
- return {int(k): v for k, v in x.items()}
- # Key - channels id
- # Value - anchored message id
- # If using cogs you can make it a botvar with bot.anchored_channels
- try:
- with open("anchors.json", "r") as file:
- anchored_channels = json.load(file, object_hook=keys_to_int)
- except (FileNotFoundError, json.JSONDecodeError):
- # On error just create the file
- with open("anchors.json", "w") as file: pass
- anchored_channels = {}
- @bot.event
- async def on_ready():
- print(f"Successfully logged in and booted...!")
- @bot.command()
- async def help(ctx):
- # This send in the channel the command was invoked
- await ctx.send("__cheack in your DMs.__")
- # This sends to DM of user who invoked it
- await ctx.author.send("``!anchor`` ``message`` - makes an achor mesage with your message.")
- await ctx.author.send("``!help`` - shows how to use this bot.")
- await ctx.author.delete(delay=0)
- @bot.command()
- async def anchor(ctx, *, message):
- anchored_channel = ctx.channel
- # Construct embed
- embed = discord.Embed(title="Anchor", description=message, color=0x00ff00)
- # We make the bot send the message to the channel this command was called
- # Message will have contents of *message* agument we passed to the command
- anchored_message = await anchored_channel.send(embed=embed)
- # We save the anchor message ID
- anchored_channels[anchored_channel.id] = anchored_message.id
- # Delete the message command for tidiness
- await ctx.message.delete(delay=0)
- @anchor.error
- async def anchor_handler(ctx, error):
- # Check if our required argument message is missing.
- if isinstance(error, commands.MissingRequiredArgument):
- await ctx.send("You forgot to give me message that will serve as anchor!")
- @bot.event
- async def on_message(message):
- # Ignore messages from self(bot) to avoid infinite loop
- if message.author != message.guild.me:
- for anchored_chanel_id in anchored_channels:
- if message.channel.id == anchored_chanel_id:
- # We fetch the message that was set as anchor in this channel
- anchored_message = await message.channel.fetch_message(anchored_channels[anchored_chanel_id])
- # We delete it and re-send it to the same channel, thus making in anchored aka
- # the last message sent in that channel
- await anchored_message.delete()
- # Construct an embed
- embed = discord.Embed(title="Anchor", description=anchored_message.embeds[0].description, colour=random.randint(0, 16777216))
- updated_anchor_msg = await message.channel.send(embed=embed)
- # Since we deleted the message the saved ID won't work since IDs are unique
- # We just update the ID from new message object
- update_anchored_channels(anchored_chanel_id, updated_anchor_msg.id)
- # Overriding the default provided on_message forbids any extra commands from running.
- # To fix this, we add client.process_commands(message) line at the end of our on_message
- await bot.process_commands(message)
- def update_anchored_channels(channel_id, message_id):
- anchored_channels[channel_id] = message_id
- with open("anchors.json", "w") as file:
- json.dump(anchored_channels, file)
- bot.run('')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement