From 0b6792594f3cf37b85eff16e6a40dd3cd839d6ce Mon Sep 17 00:00:00 2001 From: Simon-Noble Date: Sun, 30 Jun 2024 11:56:53 -0400 Subject: [PATCH 1/4] Turned all commands in matching.py into hybrid commands. This should maintain current functionality and add the ability to use slash commands. --- matching.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/matching.py b/matching.py index d190cd6..bcb1c37 100644 --- a/matching.py +++ b/matching.py @@ -303,13 +303,13 @@ def __init__(self, bot): 'creation_msg_id'], invites_sent=v['invites_sent']) asyncio.ensure_future(update_timer()) - @commands.command() + @commands.hybrid_command() async def help(self, ctx, arg=''): if ctx.channel.id in channels: await ctx.channel.send(files.get_help(arg), allowed_mentions=mentions) - @commands.command(aliases=['new']) + @commands.hybrid_command(aliases=['new']) async def lfg(self, ctx, *, text): print(f'!lfg {text}') print(f'creation_msg: {ctx.message.jump_url}') @@ -332,14 +332,14 @@ async def lfg(self, ctx, *, text): await match.message.add_reaction(emoji_str) await match.update() - @commands.command(aliases=['remove']) + @commands.hybrid_command(aliases=['remove']) async def delete(self, ctx): for match in matches.values(): if match.host == ctx.author.id: match_to_delete = match match_to_delete.mark_delete() - @commands.command() + @commands.hybrid_command() async def edit(self, ctx, *, text): for match in matches.values(): if match.host == ctx.author.id: @@ -348,7 +348,7 @@ async def edit(self, ctx, *, text): match_to_edit.edit_note(text) await match.update() - @commands.command(aliases=edit_date_aliases) + @commands.hybrid_command(aliases=edit_date_aliases) async def edit_date(self, ctx, *, text): for match in matches.values(): if match.host == ctx.author.id: @@ -356,7 +356,7 @@ async def edit_date(self, ctx, *, text): match_to_edit.edit_time(text) await match.update() - @commands.command(aliases=edit_text_aliases) + @commands.hybrid_command(aliases=edit_text_aliases) async def edit_text(self, ctx, *, text): for match in matches.values(): if match.host == ctx.author.id: From 192bfd834426432aadc6f63af716e04cf975e4f6 Mon Sep 17 00:00:00 2001 From: Simon-Noble Date: Sun, 30 Jun 2024 20:40:26 -0400 Subject: [PATCH 2/4] Added syncing to make sure the slash commands are properly added --- main.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/main.py b/main.py index 0bacece..1c9714f 100644 --- a/main.py +++ b/main.py @@ -33,6 +33,11 @@ async def on_ready(): await bot.add_cog(audit.Audit(bot)) await bot.add_cog(voice.Voice(bot)) await bot.add_cog(pin.Pin(bot)) + try: + synced = await bot.tree.sync() + print(f"Synced {len(synced)} commands.") + except Exception as e: + print(e) @bot.command() From 2c56c917102630e2d2a44a713219c5c6208e847f Mon Sep 17 00:00:00 2001 From: Simon-Noble Date: Sun, 30 Jun 2024 21:07:54 -0400 Subject: [PATCH 3/4] Properly replies to slash commands, minor refactoring to the token and to main.py --- main.py | 116 ++++++++++++++++++++++++++++------------------------ matching.py | 71 +++++++++++++++++++++----------- 2 files changed, 109 insertions(+), 78 deletions(-) diff --git a/main.py b/main.py index 1c9714f..eb2f9d1 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +import json import logging import discord @@ -10,68 +11,75 @@ import voice import pin -logger = logging.getLogger('discord') -logger.setLevel(logging.DEBUG) -file_handler = logging.FileHandler(filename='discord.log', encoding='utf-8', - mode='w') -file_handler.setFormatter(logging.Formatter( - '%(asctime)s:%(levelname)s:%(name)s: %(message)s')) -logger.addHandler(file_handler) -intents = discord.Intents(messages=True, guilds=True, reactions=True, - members=True, message_content=True) -bot = commands.Bot(command_prefix='!', intents=intents, case_insensitive=True, - help_command=None, - allowed_mentions=discord.AllowedMentions(everyone=False)) +def get_token() -> str: + with open("token.json") as f: + file = f.read() + token = json.loads(file)["token"] + return token -@bot.event -async def on_ready(): - print('We have logged in as {0.user}'.format(bot)) - print('Time: {0}'.format(datetime.datetime.now(datetime.timezone.utc))) - await bot.add_cog(matching.Matching(bot)) - await bot.add_cog(roles.Roles(bot)) - await bot.add_cog(audit.Audit(bot)) - await bot.add_cog(voice.Voice(bot)) - await bot.add_cog(pin.Pin(bot)) - try: - synced = await bot.tree.sync() - print(f"Synced {len(synced)} commands.") - except Exception as e: - print(e) +def main(): + logger = logging.getLogger('discord') + logger.setLevel(logging.DEBUG) + file_handler = logging.FileHandler(filename='discord.log', encoding='utf-8', + mode='w') + file_handler.setFormatter(logging.Formatter( + '%(asctime)s:%(levelname)s:%(name)s: %(message)s')) + logger.addHandler(file_handler) + intents = discord.Intents(messages=True, guilds=True, reactions=True, + members=True, message_content=True) + bot = commands.Bot(command_prefix='!', intents=intents, case_insensitive=True, + help_command=None, + allowed_mentions=discord.AllowedMentions(everyone=False)) -@bot.command() -async def ping(ctx): - await ctx.send('pong') - + @bot.event + async def on_ready(): + print('We have logged in as {0.user}'.format(bot)) + print('Time: {0}'.format(datetime.datetime.now(datetime.timezone.utc))) + await bot.add_cog(matching.Matching(bot)) + await bot.add_cog(roles.Roles(bot)) + await bot.add_cog(audit.Audit(bot)) + await bot.add_cog(voice.Voice(bot)) + await bot.add_cog(pin.Pin(bot)) + try: + synced = await bot.tree.sync() + print(f"Synced {len(synced)} commands.") + except Exception as e: + print(e) -@bot.command() -@is_owner() -async def msg(ctx, channel: int, *, text: str): - await bot.get_channel(channel).send(text) + @bot.command() + async def ping(ctx): + await ctx.send('pong') + @bot.command() + @is_owner() + async def msg(ctx, channel: int, *, text: str): + await bot.get_channel(channel).send(text) -@bot.command() -@is_owner() -async def delete_category(ctx): - if ctx.channel.category: - await voice.delete_category_fully(ctx.channel.category) + @bot.command() + @is_owner() + async def delete_category(ctx): + if ctx.channel.category: + await voice.delete_category_fully(ctx.channel.category) + @bot.command() + @is_owner() + async def mass_ban(ctx, *, text: str): + lines = text.splitlines() + for line in lines: + try: + user_id = int(line.split()[0][3:-1]) + member = ctx.guild.get_member(user_id) + if member: + await ctx.guild.ban(member, reason='spam bot') + print('banned: ', member) + except ValueError: + print('ValueError: ', line.split()[0]) -@bot.command() -@is_owner() -async def mass_ban(ctx, *, text: str): - lines = text.splitlines() - for line in lines: - try: - user_id = int(line.split()[0][3:-1]) - member = ctx.guild.get_member(user_id) - if member: - await ctx.guild.ban(member, reason='spam bot') - print('banned: ', member) - except ValueError: - print('ValueError: ', line.split()[0]) + bot.allowed_mentions = discord.AllowedMentions(everyone=False) + bot.run(get_token()) -bot.allowed_mentions = discord.AllowedMentions(everyone=False) -bot.run('') +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/matching.py b/matching.py index cde2c01..e5b5873 100644 --- a/matching.py +++ b/matching.py @@ -52,17 +52,17 @@ channels = { # lfg: matches - 625131638887546890: 995753350408716308, # classic dune - 900857355242184734: 995761430152368240, # dune: conquest - 801911401156968478: 995752955464650782, # dune: imperium + 625131638887546890: 995753350408716308, # classic dune + 900857355242184734: 995761430152368240, # dune: conquest + 801911401156968478: 995752955464650782, # dune: imperium 1022775514278142002: 1022775384992911410, # dune: war for arrakis - 744191730097586236: 995751425390936124, # dune off-topic - 746433956894867506: 694601128402485258, # test 1 - 746433606259572899: 746433914595311688, # test 2 - 782783616232194068: 783402464493174816, # eclipse - 778682723665051698: 778682593499414579, # chaos in the old world - 833984225824866336: 833984400257056769, # inis - 822605513623339008: 855472640847773696, # dune play-testing + 744191730097586236: 995751425390936124, # dune off-topic + 746433956894867506: 694601128402485258, # test 1 + 746433606259572899: 746433914595311688, # test 2 + 782783616232194068: 783402464493174816, # eclipse + 778682723665051698: 778682593499414579, # chaos in the old world + 833984225824866336: 833984400257056769, # inis + 822605513623339008: 855472640847773696, # dune play-testing 1032408375507566612: 1032672765070811218, # dune: imperium ranked 1257052124597194913: 1257052081802448937, # public test } @@ -308,9 +308,9 @@ def __init__(self, bot): @commands.hybrid_command() async def help(self, ctx, arg=''): - if ctx.channel.id in channels: - await ctx.channel.send(files.get_help(arg), - allowed_mentions=mentions) + await ctx.reply(files.get_help(arg), + allowed_mentions=mentions, ) + @commands.hybrid_command(aliases=['new']) async def lfg(self, ctx, *, text): @@ -329,44 +329,67 @@ async def lfg(self, ctx, *, text): embed=embed, allowed_mentions=mentions)) matches[match.message.id] = match match.save() - for emoji_id in custom_emojis_to_add: - emoji = await self.guild.fetch_emoji(emoji_id) - await match.message.add_reaction(emoji) + #for emoji_id in custom_emojis_to_add: + # emoji = await self.guild.fetch_emoji(emoji_id) + # await match.message.add_reaction(emoji) for emoji_str in common_emojis_to_add: await match.message.add_reaction(emoji_str) await match.update() + await ctx.reply("Match created!") @commands.hybrid_command(aliases=['remove']) async def delete(self, ctx): + match_to_delete = None for match in matches.values(): if match.host == ctx.author.id: match_to_delete = match - match_to_delete.mark_delete() + + if match_to_delete: + match_to_delete.mark_delete() + await ctx.reply("Match deleted successfully!") + return + await ctx.reply("Something went wrong deleting the match.") @commands.hybrid_command() async def edit(self, ctx, *, text): + match_to_edit = None for match in matches.values(): if match.host == ctx.author.id: match_to_edit = match - match_to_edit.edit_time(text) - match_to_edit.edit_note(text) - await match.update() + if match_to_edit: + match_to_edit.edit_time(text) + match_to_edit.edit_note(text) + await match_to_edit.update() + await ctx.reply("Match edited successfully!") + return + await ctx.reply("Something went wrong editing the match.") @commands.hybrid_command(aliases=edit_date_aliases) async def edit_date(self, ctx, *, text): + match_to_edit = None for match in matches.values(): if match.host == ctx.author.id: match_to_edit = match - match_to_edit.edit_time(text) - await match.update() + if match_to_edit: + match_to_edit.edit_time(text) + await match_to_edit.update() + await ctx.reply("Match edited successfully!") + return + await ctx.reply("Something went wrong editing the match.") @commands.hybrid_command(aliases=edit_text_aliases) async def edit_text(self, ctx, *, text): + match_to_edit = None for match in matches.values(): if match.host == ctx.author.id: match_to_edit = match - match_to_edit.edit_note(text) - await match.update() + if match_to_edit: + match_to_edit.edit_note(text) + await match_to_edit.update() + await ctx.reply("Match edited successfully!") + return + + await ctx.reply("Something went wrong editing the match.") @commands.Cog.listener() async def on_raw_reaction_add(self, payload): From 96e5c034c5bc44030e9357444d2a6f31eea135dd Mon Sep 17 00:00:00 2001 From: Simon-Noble Date: Fri, 13 Sep 2024 12:42:03 -0400 Subject: [PATCH 4/4] Fixes for errors thrown on match creation --- matching.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/matching.py b/matching.py index e5b5873..7dbd557 100644 --- a/matching.py +++ b/matching.py @@ -408,26 +408,32 @@ async def on_raw_reaction_add(self, payload): if user_id == match.get_host(): print('mark delete: {0}'.format(msg_id)) match.mark_delete() + elif self.bot.get_user(user_id).bot: + pass else: error_message = "You cannot delete this match because " \ "you are not its creator!" await self.bot.get_user(user_id).send(error_message) elif emojis[emoji] == edit: - if user_id == match.get_host(): - message = "✍️Edit your most recent match with !text, " \ - "!time, or !edit. For example:\n" - message += "!text Vanilla Advanced -> match text " \ - "becomes 'Vanilla Advanced'\n" - message += "!time tomorrow, 10pm UTC+1 -> match time " \ - "becomes tomorrow, 10pm UTC+1\n" - message += "!edit tomorrow, 10pm UTC+1 ! Vanilla " \ - "Advanced -> match time becomes tomorrow, " \ - "10pm UTC+1 and match text becomes 'tomorrow," \ - " 10pm UTC+1 ! Vanilla Advanced'" + if self.bot.get_user(user_id).bot: + pass else: - message = "You cannot edit this match because " \ - "you are not its creator!" - await self.bot.get_user(user_id).send(message) + if user_id == match.get_host(): + message = "✍️Edit your most recent match with !text, " \ + "!time, or !edit. For example:\n" + message += "!text Vanilla Advanced -> match text " \ + "becomes 'Vanilla Advanced'\n" + message += "!time tomorrow, 10pm UTC+1 -> match time " \ + "becomes tomorrow, 10pm UTC+1\n" + message += "!edit tomorrow, 10pm UTC+1 ! Vanilla " \ + "Advanced -> match time becomes tomorrow, " \ + "10pm UTC+1 and match text becomes 'tomorrow," \ + " 10pm UTC+1 ! Vanilla Advanced'" + + else: + message = "You cannot edit this match because " \ + "you are not its creator!" + await self.bot.get_user(user_id).send(message) @commands.Cog.listener() async def on_raw_reaction_remove(self, payload):