Skip to content

Commit

Permalink
Initial animepost command
Browse files Browse the repository at this point in the history
  • Loading branch information
janvernieuwe committed Nov 21, 2022
1 parent 03a5f68 commit 6a43795
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 5 deletions.
8 changes: 3 additions & 5 deletions extensions/anime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand All @@ -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):
Expand Down
62 changes: 62 additions & 0 deletions extensions/animeforum.py
Original file line number Diff line number Diff line change
@@ -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('<br>', "\n")\
.replace('<b>', '**')\
.replace('</b>', '**')\
.replace('<i>', '*')\
.replace('</i>', '*')

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)
40 changes: 40 additions & 0 deletions extensions/test.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 6a43795

Please sign in to comment.