From 6a43795db74dc57154f09f530fa3fe87b01a9672 Mon Sep 17 00:00:00 2001 From: Jan Vernieuwe Date: Mon, 21 Nov 2022 19:26:31 +0100 Subject: [PATCH] Initial animepost command --- extensions/anime.py | 8 ++---- extensions/animeforum.py | 62 ++++++++++++++++++++++++++++++++++++++++ extensions/test.py | 40 ++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 extensions/animeforum.py create mode 100644 extensions/test.py diff --git a/extensions/anime.py b/extensions/anime.py index 388a634..6940b08 100644 --- a/extensions/anime.py +++ b/extensions/anime.py @@ -15,13 +15,9 @@ session = CacheControl(requests.Session(), heuristic=expires, cache=FileCache(config.cache_dir)) @commands.hybrid_command(help='Show anime information') -async def anime(interaction: discord.Interaction, search): +async def anime(ctx: discord.ext.commands.context.Context, search): anilist = Anilist() anime_id = anilist.get_anime_id(search) - ctx = interaction - if(anime_id == -1): - ctx.channel.send_help('test') - return anime = anilist.get_anime(search) description_parts = textwrap.wrap(anime['desc'], 1000) embed = discord.Embed(type='rich', title=anime['name_romaji']) @@ -38,6 +34,8 @@ async def anime(interaction: discord.Interaction, 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() async def setup(bot): diff --git a/extensions/animeforum.py b/extensions/animeforum.py new file mode 100644 index 0000000..768f978 --- /dev/null +++ b/extensions/animeforum.py @@ -0,0 +1,62 @@ +import re + +from discord import ChannelType, TextStyle, ForumChannel, 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 + + +class AnimeForm(Modal): + def __init__(self, anime, anilist_link): + self.name_preview = anime['name_english'] or anime['name_romaji'] + super().__init__(title=f'Create post for {self.name_preview}'[0:45]) # Modal title + self.anime = anime + #print(anime) + self.anilist_link = anilist_link + self.name = TextInput(label='name', placeholder="https://www.youtube.com/watch?v=dQw4w9WgXcQ", required=True, default=self.name_preview[0:100]) + self.youtube = TextInput(label='Youtube trailer link', placeholder="https://www.youtube.com/watch?v=dQw4w9WgXcQ", required=False) + + self.add_item(self.name) + self.add_item(self.youtube) + + def filter_tags(self, tag: ForumTag): + return tag.name in self.anime['genres'] + + 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('', '*') + + 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, + content=content, + reason=f"Anime thread created by {interaction.user.name}" + ) + tags = [] + filtered = filter(self.filter_tags, forum.available_tags) + for tag in filtered: + 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']) +async def animepost(ctx: Context, anilist_link): + anilist_id = re.search(r'anime/(\d+)', anilist_link)[1] + anilist = Anilist() + anime = anilist.get_anime_with_id(anilist_id) + modal = AnimeForm(anime, anilist_link) + await ctx.interaction.response.send_modal(modal) + + +async def setup(bot): + bot.add_command(animepost) diff --git a/extensions/test.py b/extensions/test.py new file mode 100644 index 0000000..c836117 --- /dev/null +++ b/extensions/test.py @@ -0,0 +1,40 @@ +import sys +import time + +from discord import ChannelType, TextStyle +from discord.ext.commands import Context + +import config +import discord +from discord.ext import commands +from discord.ui import Modal, TextInput, ChannelSelect + + +class ChannelForm(Modal): + def __init__(self): + super().__init__(title="Create a new joinable channel") # Modal title + + self.name = TextInput(label="Name") + self.add_item(self.name) + + #self.category = ChannelSelect(channel_types=[]) + self.category = TextInput(label="Category id") + self.add_item(self.category) + + self.description = TextInput(label="Description", style=TextStyle.long) + self.add_item(self.description) + + async def on_submit(self, interaction: discord.Interaction): + category = interaction.guild.get_channel(int(self.category.value)) + await interaction.response.send_message(f'Channel {self.name}, category: {category.name}, description {self.description}') + + +@commands.hybrid_command(help='Testing stuff') +@commands.has_role(config.role['global_mod']) +async def test(ctx: Context): + modal = ChannelForm() + await ctx.interaction.response.send_modal(modal) + + +async def setup(bot): + bot.add_command(test)