Advertisement
Guest User

1235462345

a guest
Jul 19th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.84 KB | None | 0 0
  1. import json
  2. import discord
  3. import random
  4. from discord.ext import commands
  5.  
  6. bot = commands.Bot(command_prefix="!")
  7. bot.remove_command("help")
  8.  
  9.  
  10. def keys_to_int(x):
  11.     """
  12.       Json doesn't allow keys as integers.
  13.       When saving we will have
  14.       {123: 567}
  15.       where 123 is anchored channel ID and 567 anchored message ID
  16.       both are int, and we need to load the as int too.
  17.       But json forces keys as strings upon saving so we need to convert them back to int
  18.   """
  19.     return {int(k): v for k, v in x.items()}
  20.  
  21.  
  22. # Key - channels id
  23. # Value - anchored message id
  24. # If using cogs you can make it a botvar with bot.anchored_channels
  25. try:
  26.     with open("anchors.json", "r") as file:
  27.         anchored_channels = json.load(file, object_hook=keys_to_int)
  28. except (FileNotFoundError, json.JSONDecodeError):
  29.     # On error just create the file
  30.     with open("anchors.json", "w") as file: pass
  31.     anchored_channels = {}
  32.  
  33.  
  34. @bot.event
  35. async def on_ready():
  36.     print(f"Successfully logged in and booted...!")
  37.  
  38.  
  39. @bot.command()
  40. async def help(ctx):
  41.     # This send in the channel the command was invoked
  42.     await ctx.send("__cheack in your DMs.__")
  43.     # This sends to DM of user who invoked it
  44.     await ctx.author.send("``!anchor`` ``message`` - makes an achor mesage with your message.")
  45.     await ctx.author.send("``!help`` - shows how to use this bot.")
  46.     await ctx.author.delete(delay=0)
  47.  
  48.  
  49. @bot.command()
  50. async def anchor(ctx, *, message):
  51.     anchored_channel = ctx.channel
  52.     # Construct embed
  53.     embed = discord.Embed(title="Anchor", description=message, color=0x00ff00)
  54.     # We make the bot send the message to the channel this command was called
  55.     # Message will have contents of *message* agument we passed to the command
  56.     anchored_message = await anchored_channel.send(embed=embed)
  57.     # We save the anchor message ID
  58.     anchored_channels[anchored_channel.id] = anchored_message.id
  59.     # Delete the message command for tidiness
  60.     await ctx.message.delete(delay=0)
  61.  
  62.  
  63. @anchor.error
  64. async def anchor_handler(ctx, error):
  65.     # Check if our required argument message is missing.
  66.     if isinstance(error, commands.MissingRequiredArgument):
  67.         await ctx.send("You forgot to give me message that will serve as anchor!")
  68.  
  69.  
  70. @bot.event
  71. async def on_message(message):
  72.     # Ignore messages from self(bot) to avoid infinite loop
  73.     if message.author != message.guild.me:
  74.         for anchored_chanel_id in anchored_channels:
  75.             if message.channel.id == anchored_chanel_id:
  76.                 # We fetch the message that was set as anchor in this channel
  77.                 anchored_message = await message.channel.fetch_message(anchored_channels[anchored_chanel_id])
  78.                 # We delete it and re-send it to the same channel, thus making in anchored aka
  79.                 # the last message sent in that channel
  80.                 await anchored_message.delete()
  81.                 # Construct an embed
  82.                 embed = discord.Embed(title="Anchor", description=anchored_message.embeds[0].description, colour=random.randint(0, 16777216))
  83.                 updated_anchor_msg = await message.channel.send(embed=embed)
  84.                 # Since we deleted the message the saved ID won't work since IDs are unique
  85.                 # We just update the ID from new message object
  86.                 update_anchored_channels(anchored_chanel_id, updated_anchor_msg.id)
  87.  
  88.     # Overriding the default provided on_message forbids any extra commands from running.
  89.     # To fix this, we add client.process_commands(message) line at the end of our on_message
  90.     await bot.process_commands(message)
  91.  
  92.  
  93. def update_anchored_channels(channel_id, message_id):
  94.     anchored_channels[channel_id] = message_id
  95.     with open("anchors.json", "w") as file:
  96.         json.dump(anchored_channels, file)
  97.  
  98. bot.run('')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement