|
| 1 | +import discord |
| 2 | +import json |
| 3 | +import os |
| 4 | +import datetime |
| 5 | +from discord.ext import commands |
| 6 | + |
| 7 | + |
| 8 | +class Bot(commands.Bot): |
| 9 | + |
| 10 | + CC_LOGO_URL = 'https://avatars.githubusercontent.com/u/63065397?v=4' |
| 11 | + INTRO_IMG_URL = 'https://user-images.githubusercontent.com/63065397/156466208-ffb6db84-f0c0-4860-ab6d-48ad0f2cd5f7.png' |
| 12 | + EXHAUSTED_FACE = 'https://user-images.githubusercontent.com/63065397/156922064-95c73c2a-b6cb-402e-b24b-d79fe7bf520a.png' |
| 13 | + DEX_YELLOW = 0x8e38ce |
| 14 | + REPOSITORY_URL = 'https://github.com/code-chaser/dex/' |
| 15 | + |
| 16 | + def __init__(self, *args, **kwargs): |
| 17 | + super().__init__( |
| 18 | + command_prefix=self.get_prefix, |
| 19 | + intents=discord.Intents.all(), |
| 20 | + activity=discord.Activity( |
| 21 | + type=discord.ActivityType.listening, |
| 22 | + name="Stop WW3!", |
| 23 | + large_image_url=self.EXHAUSTED_FACE, |
| 24 | + small_image_url=self.EXHAUSTED_FACE, |
| 25 | + start=datetime.datetime(2022, 2, 24), |
| 26 | + ), |
| 27 | + ) |
| 28 | + |
| 29 | + for file in os.listdir('./src/cogs'): |
| 30 | + if file.endswith('.py'): |
| 31 | + self.load_extension(f'src.cogs.{file[:-3]}') |
| 32 | + |
| 33 | + async def get_prefix(self, message): |
| 34 | + with open('./data/prefixes.json', 'r') as pref: |
| 35 | + prefixes = json.load(pref) |
| 36 | + print(prefixes[str(message.guild.id)]+"\n\n") |
| 37 | + return prefixes[str(message.guild.id)] + ' ' |
| 38 | + |
| 39 | + def run(self) -> None: |
| 40 | + super().run(os.getenv('BOT_TOKEN')) |
| 41 | + |
| 42 | + async def on_ready(self) -> None: |
| 43 | + print('Logged in as {0.user}'.format(self)) |
| 44 | + |
| 45 | + async def on_guild_join(self, guild) -> None: |
| 46 | + with open('./data/prefixes.json', 'r') as pref: |
| 47 | + prefixes = json.load(pref) |
| 48 | + prefixes[str(guild.id)] = '$dex' |
| 49 | + with open('./data/prefixes.json', 'w') as pref: |
| 50 | + json.dump(prefixes, pref, indent=4) |
| 51 | + with open('./data/tag_messages.json', 'r') as tag_: |
| 52 | + tag_messages = json.load(tag_) |
| 53 | + tag_messages[str(guild.id)] = 'on' |
| 54 | + with open('./data/tag_messages.json', 'w') as tag_: |
| 55 | + json.dump(tag_messages, tag_, indent=4) |
| 56 | + for channel in guild.text_channels: |
| 57 | + if channel.permissions_for(guild.me).send_messages: |
| 58 | + general = channel |
| 59 | + if general is not None: |
| 60 | + await general.send(embed=self.intro_msg_embed(guild)) |
| 61 | + |
| 62 | + async def on_guild_remove(self, guild) -> None: |
| 63 | + with open('./data/prefixes.json', 'r') as pref: |
| 64 | + prefixes = json.load(pref) |
| 65 | + if str(guild.id) in prefixes.keys(): |
| 66 | + prefixes.pop(str(guild.id)) |
| 67 | + with open('./data/prefixes.json', 'w') as pref: |
| 68 | + json.dump(prefixes, pref, indent=4) |
| 69 | + with open('./data/tag_messages.json', 'r') as tag_: |
| 70 | + tag_messages = json.load(tag_) |
| 71 | + if str(guild.id) in tag_messages.keys(): |
| 72 | + tag_messages.pop(str(guild.id)) |
| 73 | + with open('./data/tag_messages.json', 'w') as tag_: |
| 74 | + json.dump(tag_messages, tag_, indent=4) |
| 75 | + |
| 76 | + async def on_message(self, message) -> None: |
| 77 | + await self.process_commands(message) |
| 78 | + with open('./data/tag_messages.json', 'r') as tag_: |
| 79 | + tag_messages = json.load(tag_) |
| 80 | + if tag_messages[str(message.guild.id)] == 'off': |
| 81 | + return |
| 82 | + target = message.author |
| 83 | + if target == self.user or target.bot: |
| 84 | + return |
| 85 | + embed = discord.Embed( |
| 86 | + title='Message Tagged', |
| 87 | + colour=target.colour, |
| 88 | + timestamp=datetime.datetime.utcnow(), |
| 89 | + ) |
| 90 | + embed.set_footer( |
| 91 | + text=''.join('`<prefix> tags off` -to turn this off'), |
| 92 | + ) |
| 93 | + embed.add_field(name='Message', value=message.content, inline=False) |
| 94 | + embed.add_field(name='Author', value=target.mention, inline=True) |
| 95 | + embed.set_thumbnail(url=target.avatar_url) |
| 96 | + await message.channel.send(embed=embed) |
| 97 | + |
| 98 | + async def on_command_error(self, ctx, error) -> None: |
| 99 | + embed = discord.Embed( |
| 100 | + title='Status', |
| 101 | + colour=0xff0000, |
| 102 | + timestamp=datetime.datetime.utcnow(), |
| 103 | + ) |
| 104 | + if isinstance(error, commands.MissingPermissions): |
| 105 | + n = 'Error' |
| 106 | + v = 'Missing Permissions' |
| 107 | + elif isinstance(error, commands.MissingRequiredArgument): |
| 108 | + n = 'Error' |
| 109 | + v = 'Missing required arguements' |
| 110 | + elif isinstance(error, commands.MemberNotFound): |
| 111 | + n = 'Error' |
| 112 | + v = "Requested member not found or Dex doesn't have access to them" |
| 113 | + elif isinstance(error, commands.BotMissingPermissions): |
| 114 | + n = 'Error' |
| 115 | + v = 'Missing Permissions' |
| 116 | + elif isinstance(error, commands.CommandNotFound): |
| 117 | + n = 'Error' |
| 118 | + v = 'Invalid Command' |
| 119 | + else: |
| 120 | + raise error |
| 121 | + embed.add_field(name=n, value=v, inline=False) |
| 122 | + await ctx.send(embed=embed) |
| 123 | + |
| 124 | + def intro_msg_embed(self, guild): |
| 125 | + description = '' |
| 126 | + description = description.join( |
| 127 | + '\nThanks for adding me to ' + guild.name + '!') |
| 128 | + description = description.join('\nUse `$dex help` to get started!') |
| 129 | + description = description.join( |
| 130 | + '\nVisit: '.join(self.REPOSITORY_URL)) |
| 131 | + embed = discord.Embed( |
| 132 | + title='**GREETINGS!**', |
| 133 | + description=description, |
| 134 | + color=self.DEX_YELLOW, |
| 135 | + timestamp=datetime.datetime.utcnow(), |
| 136 | + ) |
| 137 | + embed.set_image(url=self.INTRO_IMG_URL) |
| 138 | + embed.set_author( |
| 139 | + name='dex', |
| 140 | + url=self.REPOSITORY_URL, |
| 141 | + icon_url=self.user.avatar_url, |
| 142 | + ) |
| 143 | + embed.set_footer( |
| 144 | + text='made by codechaser', |
| 145 | + icon_url=self.CC_LOGO_URL, |
| 146 | + ) |
| 147 | + embed.set_thumbnail(url=guild.icon_url) |
| 148 | + return embed |
0 commit comments