From 206360b7ba9fded34047e8509c68f8f9c0ab8bce Mon Sep 17 00:00:00 2001 From: Jan Vernieuwe Date: Mon, 21 Nov 2022 21:55:06 +0100 Subject: [PATCH] Make the commands interaction compatible and disable the broken ones --- cogs/bikkelpunt.py | 14 ++++++----- cogs/channels.py | 14 ++++++----- cogs/{cots.py => cots.py.disabled} | 0 cogs/sotw.py | 6 ++--- extensions/anilist.py | 1 + extensions/{anime.py => anime.py.disabled} | 3 +-- extensions/{animeforum.py => animepost.py} | 23 +++++++++++-------- extensions/credits.py | 2 +- ...emojiexport.py => emojiexport.py.disabled} | 0 extensions/leave.py | 1 + extensions/{test.py => test.py.disabled} | 0 .../{trailer.py => trailer.py.disabled} | 0 .../{userexport.py => userexport.py.disabled} | 2 +- startdiscord.sh | 2 +- util/html2md.py | 10 ++++++++ 15 files changed, 48 insertions(+), 30 deletions(-) rename cogs/{cots.py => cots.py.disabled} (100%) rename extensions/{anime.py => anime.py.disabled} (95%) rename extensions/{animeforum.py => animepost.py} (79%) rename extensions/{emojiexport.py => emojiexport.py.disabled} (100%) rename extensions/{test.py => test.py.disabled} (100%) rename extensions/{trailer.py => trailer.py.disabled} (100%) rename extensions/{userexport.py => userexport.py.disabled} (94%) create mode 100644 util/html2md.py diff --git a/cogs/bikkelpunt.py b/cogs/bikkelpunt.py index e757285..f4f9239 100644 --- a/cogs/bikkelpunt.py +++ b/cogs/bikkelpunt.py @@ -90,8 +90,9 @@ async def get(self, ctx): # first check if the time is right before doing anything else check_time = self.utils.is_time_correct() if check_time is False: - return await ctx.channel.send( - f":last_quarter_moon_with_face: Echte bikkels leven tussen 03:00 en 05:00!" + return await ctx.interaction.response.send_message( + f":last_quarter_moon_with_face: Echte bikkels leven tussen 03:00 en 05:00!", + ephemeral=True ) record = self.utils.get_existing_record(ctx.message.author.id) if record is None: @@ -99,11 +100,12 @@ async def get(self, ctx): record = self.utils.get_existing_record(ctx.message.author.id) else: if self.utils.has_cooldown(record): - return await ctx.channel.send( - f":last_quarter_moon_with_face: Je hebt al gebikkelt vandaag, probeer het morgen nog eens" + return await ctx.interaction.response.send_message( + f":last_quarter_moon_with_face: Je hebt al gebikkelt vandaag, probeer het morgen nog eens", + ephemeral=True ) self.utils.update_bikkelpunt_record(ctx.message, record.get('points')) - await ctx.channel.send( + await ctx.interaction.response.send_message( f":last_quarter_moon_with_face: Je bent een echte bikkel! " f"**+1** (**{record.get('points') + 1}** punten totaal)" ) @@ -111,7 +113,7 @@ async def get(self, ctx): @bikkel.command(help='Toon de top 10 bikkelpunten') async def ranking(self, ctx): database.reconnect() - await ctx.channel.send(self.utils.get_top_10_message()) + await ctx.interaction.response.send_message(self.utils.get_top_10_message()) async def setup(bot): diff --git a/cogs/channels.py b/cogs/channels.py index efdd80b..d15d5d6 100644 --- a/cogs/channels.py +++ b/cogs/channels.py @@ -8,7 +8,7 @@ from cachecontrol import CacheControl from cachecontrol.heuristics import ExpiresAfter from cachecontrol.caches.file_cache import FileCache -from util.confirm import Confirm +from discord.ext.commands import Context import config @@ -143,8 +143,8 @@ async def _joinmessage(channel, embed) -> discord.message: await msg.add_reaction('⏹') return msg - @commands.hybrid_command(pass_context=True, help='Create a joinable anime channel') - @commands.has_any_role(config.role['global_mod'], config.role['anime_mod']) + #@commands.hybrid_command(pass_context=True, help='Create a joinable anime channel') + #@commands.has_any_role(config.role['global_mod'], config.role['anime_mod']) async def animechannel(self, ctx, channel_name, mal_anime_url): print(f'{ctx.author} creates anime channel {channel_name}') guild = ctx.message.guild @@ -171,7 +171,7 @@ async def animechannel(self, ctx, channel_name, mal_anime_url): @commands.hybrid_command(pass_context=True, help='Create a simple joinable channel (use quotes for description)') @commands.has_role(config.role['global_mod']) - async def simplechannel(self, ctx, categoryid, name, description): + async def simplechannel(self, ctx: Context, categoryid, name, description): print(f'{ctx.author} creates simple channel {name} in category {categoryid}') guild = ctx.message.guild try: @@ -179,7 +179,7 @@ async def simplechannel(self, ctx, categoryid, name, description): except StopIteration: await ctx.channel.send(f':x: Cant find category <#{categoryid}> :thinking:') return - newchan = await guild.create_text_channel( + new_channel = await guild.create_text_channel( name=name, category=category, topic=description, @@ -187,8 +187,9 @@ async def simplechannel(self, ctx, categoryid, name, description): reason=f"Aangevraagd door {ctx.author}", overwrites=self.get_overwites(guild, category) ) - embed = JoinableMessage.create_simple_embed(newchan, 0) + embed = JoinableMessage.create_simple_embed(new_channel, 0) await self._joinmessage(ctx.channel, embed) + await ctx.interaction.response.send_message(f'Created channel <#{new_channel.id}> in <#{new_channel.category.id}>') @commands.Cog.listener(name='on_raw_reaction_add') async def join(self, payload): @@ -267,6 +268,7 @@ async def rechannel(self, ctx, channelid): message = JoinableMessage(message, self.bot) await message.update_members() print(f'user {ctx.author} restored {channel}') + await ctx.interaction.response.send_message('Done', ephemeral=True) async def setup(bot): diff --git a/cogs/cots.py b/cogs/cots.py.disabled similarity index 100% rename from cogs/cots.py rename to cogs/cots.py.disabled diff --git a/cogs/sotw.py b/cogs/sotw.py index e0a1588..4870d95 100644 --- a/cogs/sotw.py +++ b/cogs/sotw.py @@ -178,14 +178,13 @@ async def next(self, ctx): # Check if we have enough nominations and if we have a solid win if len(nominations) < 2: - return await ctx.channel.send(':x: Niet genoeg nominations') + return await ctx.interaction.response.send_message(':x: Niet genoeg nominations', ephemeral=True) if nominations[0].votes == nominations[1].votes: - return await ctx.channel.send(':x: Het is een gelijke stand') + return await ctx.interaction.response.send_message(':x: Het is een gelijke stand', ephemeral=True) # Build a dict of the winner for the win message and database insertion winner = nominations[0] await ctx.channel.send(await self.forum(nominations)) - # Send the win message await channel.send( f":trophy: De winnaar van week {self.get_previous_week_number()} is: " @@ -229,6 +228,7 @@ async def next(self, ctx): # Commit change database.commit() + await ctx.interaction.response.send_message('Done', ephemeral=True) async def setup(bot): diff --git a/extensions/anilist.py b/extensions/anilist.py index 5d7685e..29da1e9 100644 --- a/extensions/anilist.py +++ b/extensions/anilist.py @@ -49,6 +49,7 @@ async def anilist(ctx, username): embed.add_field(name=':busts_in_silhouette: Last Activity', value=time_string) await ctx.channel.send(embed=embed) + await ctx.interaction.response.send_message('Done', ephemeral=True) async def request_activity_data(userid): diff --git a/extensions/anime.py b/extensions/anime.py.disabled similarity index 95% rename from extensions/anime.py rename to extensions/anime.py.disabled index 6940b08..bb62fb2 100644 --- a/extensions/anime.py +++ b/extensions/anime.py.disabled @@ -34,8 +34,7 @@ async def anime(ctx: discord.ext.commands.context.Context, search): embed.add_field(name=f'Premiered', value=anime['starting_time']) embed.add_field(name=f'Links', value=f"[AniList](https://anilist.co/anime/{anime_id})") await ctx.channel.send(embed=embed) - print(ctx.interaction) - #await ctx.message.delete() + await ctx.interaction.response.send_message('Done', ephemeral=True) async def setup(bot): diff --git a/extensions/animeforum.py b/extensions/animepost.py similarity index 79% rename from extensions/animeforum.py rename to extensions/animepost.py index 768f978..3e8cac1 100644 --- a/extensions/animeforum.py +++ b/extensions/animepost.py @@ -1,13 +1,14 @@ import re -from discord import ChannelType, TextStyle, ForumChannel, ForumTag +from discord import ForumTag from discord.ext.commands import Context from AnilistPython import Anilist import config import discord from discord.ext import commands -from discord.ui import Modal, TextInput, ChannelSelect +from discord.ui import Modal, TextInput +from util.html2md import html2md class AnimeForm(Modal): @@ -28,12 +29,7 @@ def filter_tags(self, tag: ForumTag): async def on_submit(self, interaction: discord.Interaction): forum = interaction.guild.get_channel(config.channel["anime_forum"]) - description = self.anime['desc'].replace('
', "\n")\ - .replace('', '**')\ - .replace('', '**')\ - .replace('', '*')\ - .replace('', '*') - + description = html2md(self.anime['desc']) content = f'**description:** {description}\n**Start date:** {self.anime["starting_time"]}\n{self.anime["cover_image"]}\n<{self.anilist_link}>\n{self.youtube.value}' thread = await forum.create_thread( name=self.name.value, @@ -43,19 +39,26 @@ async def on_submit(self, interaction: discord.Interaction): tags = [] filtered = filter(self.filter_tags, forum.available_tags) for tag in filtered: + if len(tags) >= 5: + break tags.append(tag) await thread[0].add_tags(*tags) await interaction.response.send_message(f'Created <#{thread[0].id}> in <#{forum.id}>') @commands.hybrid_command(help='Create an anime post') -@commands.has_role(config.role['global_mod']) +@commands.has_role(config.role['user']) async def animepost(ctx: Context, anilist_link): - anilist_id = re.search(r'anime/(\d+)', anilist_link)[1] + try: + anilist_id = re.search(r'anime/(\d+)', anilist_link)[1] + except TypeError: + await ctx.interaction.response.send_message(':x: Invalid anilist url', ephemeral=True) + return anilist = Anilist() anime = anilist.get_anime_with_id(anilist_id) modal = AnimeForm(anime, anilist_link) await ctx.interaction.response.send_modal(modal) + print(f'Created anime post for {modal.name} by {ctx.interaction.user.name}') async def setup(bot): diff --git a/extensions/credits.py b/extensions/credits.py index 5299115..5946aae 100644 --- a/extensions/credits.py +++ b/extensions/credits.py @@ -19,7 +19,7 @@ async def credits(ctx): mins = int(runtime // 60 % 60) seconds = int(runtime % 60) runtime = "{} days, {} hours, {} mins, {} seconds".format(days, hours, mins, seconds) - await ctx.send(f"**Info**\n- Author: {author}\n- Library: {lib}\n- Runtime: Python {appsoft}\n- Uptime: {runtime}") + await ctx.interaction.response.send_message(f"**Info**\n- Author: {author}\n- Library: {lib}\n- Runtime: Python {appsoft}\n- Uptime: {runtime}", ephemeral=True) print(f"{ctx.author} showed the bot credits <3") diff --git a/extensions/emojiexport.py b/extensions/emojiexport.py.disabled similarity index 100% rename from extensions/emojiexport.py rename to extensions/emojiexport.py.disabled diff --git a/extensions/leave.py b/extensions/leave.py index 6fb2717..992330e 100644 --- a/extensions/leave.py +++ b/extensions/leave.py @@ -21,6 +21,7 @@ async def leave(ctx): ) await channel.send(f":outbox_tray: {user.mention} left") print(f'{user} left channel {channel} using the leave command') + await ctx.interaction.response.send_message('Done', ephemeral=True) async def setup(bot): diff --git a/extensions/test.py b/extensions/test.py.disabled similarity index 100% rename from extensions/test.py rename to extensions/test.py.disabled diff --git a/extensions/trailer.py b/extensions/trailer.py.disabled similarity index 100% rename from extensions/trailer.py rename to extensions/trailer.py.disabled diff --git a/extensions/userexport.py b/extensions/userexport.py.disabled similarity index 94% rename from extensions/userexport.py rename to extensions/userexport.py.disabled index b273c8b..ed3f366 100644 --- a/extensions/userexport.py +++ b/extensions/userexport.py.disabled @@ -30,7 +30,7 @@ async def userexport(ctx, channel_id: int = 0): binary = io.BytesIO(output.getvalue().encode('utf-8')) await ctx.send(f'Here is your export {ctx.author.mention}', file=File(binary, f"{channel.name}.csv")) print(f"{ctx.author} exported users for channel {channel}") - + await ctx.interaction.response.send_message('Done', ephemeral=True) async def setup(bot): bot.add_command(userexport) diff --git a/startdiscord.sh b/startdiscord.sh index 8241a9c..3d75b13 100755 --- a/startdiscord.sh +++ b/startdiscord.sh @@ -1,6 +1,6 @@ #!/bin/bash ## Simple shell script that reboots the bot if it crashes -until python3 discordpy.py; do +until python3 discordpy.py -v; do echo $(date +%H:%M:%S_%Y-%m-%d) "CRASH" sleep 1 done diff --git a/util/html2md.py b/util/html2md.py new file mode 100644 index 0000000..2be0d3c --- /dev/null +++ b/util/html2md.py @@ -0,0 +1,10 @@ +import string + + +def html2md(content: string): + return content.replace('\n', '')\ + .replace('
', "\n")\ + .replace('', '**')\ + .replace('', '**')\ + .replace('', '*')\ + .replace('', '*')