diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/.idea/.gitignore b/.idea/.gitignore old mode 100644 new mode 100755 diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml old mode 100644 new mode 100755 diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml old mode 100644 new mode 100755 diff --git a/.idea/misc.xml b/.idea/misc.xml old mode 100644 new mode 100755 diff --git a/.idea/modules.xml b/.idea/modules.xml old mode 100644 new mode 100755 diff --git a/.idea/vcs.xml b/.idea/vcs.xml old mode 100644 new mode 100755 diff --git a/.idea/xanathar.iml b/.idea/xanathar.iml index 74d515a027de98657e9d3d5f0f1831882fd81374..f9707ccca5ad0d6351a37cb3b72e63139204afdf 100644 --- a/.idea/xanathar.iml +++ b/.idea/xanathar.iml @@ -4,7 +4,7 @@ <content url="file://$MODULE_DIR$"> <excludeFolder url="file://$MODULE_DIR$/venv" /> </content> - <orderEntry type="inheritedJdk" /> + <orderEntry type="jdk" jdkName="Python 3.10 (xanathar)" jdkType="Python SDK" /> <orderEntry type="sourceFolder" forTests="false" /> </component> </module> \ No newline at end of file diff --git a/cogs/appointments.py b/cogs/appointments.py old mode 100644 new mode 100755 index 44f537d28c4e04858d58f71bf1c516a8987428fa..6c069e47f58980a0125725d6b8f2c27f7e44a183 --- a/cogs/appointments.py +++ b/cogs/appointments.py @@ -5,7 +5,8 @@ import json import os import uuid -import discord +from discord import app_commands, errors, Embed, File, Interaction +from discord.app_commands import Choice from discord.ext import tasks, commands import utils @@ -52,7 +53,8 @@ def get_ics_file(title, date_time, reminder, recurring): return ics_file -class Appointments(commands.Cog): +@app_commands.guild_only() +class Appointments(commands.GroupCog, name="appointments", description="Handle Appointments in Channels"): def __init__(self, bot): self.bot = bot self.fmt = os.getenv("DISCORD_DATE_TIME_FORMAT") @@ -84,15 +86,15 @@ class Appointments(commands.Cog): message = await channel.fetch_message(int(message_id)) reactions = message.reactions diff = int(round(((date_time - now).total_seconds() / 60), 0)) - answer = f"Benachrichtigung!\nDer Termin \"{appointment['title']}\" ist " + answer = f"Benachrichtigung!\nDer Termin \"{appointment['title']}\" startet" if appointment["reminder"] > 0 and diff > 0: - answer += f"startet <t:{int(date_time.timestamp())}:R>." + answer += f"<t:{int(date_time.timestamp())}:R>." if (reminder := appointment.get("reminder")) and appointment.get("recurring"): appointment["original_reminder"] = str(reminder) appointment["reminder"] = 0 else: - answer += f"jetzt fällig. :loudspeaker: " + answer += f"jetzt!!! :loudspeaker: " delete.append(message_id) answer += f"\n" @@ -106,7 +108,7 @@ class Appointments(commands.Cog): if str(message.id) in delete: await message.delete() - except discord.errors.NotFound: + except errors.NotFound: delete.append(message_id) if len(delete) > 0: @@ -135,9 +137,16 @@ class Appointments(commands.Cog): async def before_timer(self): await asyncio.sleep(60 - datetime.datetime.now().second) - @commands.command(name="add-appointment") - async def cmd_add_appointment(self, ctx, date, time, reminder, title, recurring: int = None): - await self.add_appointment(ctx.channel, ctx.author.id, date, time, reminder, title, recurring) + @app_commands.command(name="add", description="Füge dem Kanal einen neuen Termin hinzu.") + @app_commands.describe(date="Tag des Termins", time="Uhrzeit des Termins", + reminder="Wie viele Minuten bevor der Termin startet, soll eine Erinnerung verschickt werden?", + title="Titel des Termins", + recurring="In welchem Intervall (in Tagen) soll der Termin wiederholt werden?") + async def cmd_add_appointment(self, interaction: Interaction, date: str, time: str, reminder: int, title: str, + recurring: int = None): + """ Add an appointment to a channel """ + await self.add_appointment(interaction.channel, interaction.user.id, date, time, reminder, title, recurring) + await interaction.response.send_message("Termin erfolgreich erstellt!", ephemeral=True) async def add_appointment(self, channel, author_id, date, time, reminder, title, recurring: int = None): """ Add appointment to a channel """ @@ -148,17 +157,11 @@ class Appointments(commands.Cog): await channel.send("Fehler! Ungültiges Datums und/oder Zeit Format!") return - if not utils.is_valid_time(reminder): - await channel.send("Fehler! Benachrichtigung in ungültigem Format!") - return - else: - reminder = utils.to_minutes(reminder) - - embed = discord.Embed(title="Neuer Termin hinzugefügt!", - description=f"Wenn du eine Benachrichtigung zum Beginn des Termins" - f"{f', sowie {reminder} Minuten vorher, ' if reminder > 0 else f''} " - f"erhalten möchtest, reagiere mit :thumbsup: auf diese Nachricht.", - color=19607) + embed = Embed(title="Neuer Termin hinzugefügt!", + description=f"Wenn du eine Benachrichtigung zum Beginn des Termins" + f"{f', sowie {reminder} Minuten vorher, ' if reminder > 0 else f''} " + f"erhalten möchtest, reagiere mit :thumbsup: auf diese Nachricht.", + color=19607) embed.add_field(name="Titel", value=title, inline=False) embed.add_field(name="Startzeitpunkt", value=f"{date_time.strftime(self.fmt)}", inline=False) @@ -167,8 +170,8 @@ class Appointments(commands.Cog): if recurring: embed.add_field(name="Wiederholung", value=f"Alle {recurring} Tage", inline=False) - message = await channel.send(embed=embed, file=discord.File(get_ics_file(title, date_time, reminder, recurring), - filename=f"{title}.ics")) + message = await channel.send(embed=embed, file=File(get_ics_file(title, date_time, reminder, recurring), + filename=f"{title}.ics")) await message.add_reaction("ðŸ‘") await message.add_reaction("🗑ï¸") @@ -195,7 +198,7 @@ class Appointments(commands.Cog): message = await ctx.channel.fetch_message(int(message_id)) answer += f'{appointment["date_time"]}: {appointment["title"]} => ' \ f'{message.jump_url}\n' - except discord.errors.NotFound: + except errors.NotFound: delete.append(message_id) if len(delete) > 0: @@ -234,3 +237,7 @@ class Appointments(commands.Cog): message = await channel.fetch_message(payload.message_id) if len(message.embeds) > 0 and message.embeds[0].title == "Neuer Termin hinzugefügt!": await self.handle_reactions(payload) + + +async def setup(bot: commands.Bot) -> None: + await bot.add_cog(Appointments(bot)) diff --git a/cogs/components/poll/poll.py b/cogs/components/poll/poll.py old mode 100644 new mode 100755 diff --git a/cogs/polls.py b/cogs/polls.py old mode 100644 new mode 100755 index bb0b4b15017f3aac3feef34593b34ad4db36e7e1..86557d7e9e7445c465b7790cb14896ac39b2d6aa --- a/cogs/polls.py +++ b/cogs/polls.py @@ -11,7 +11,7 @@ class Polls(commands.Cog): @commands.group(name="poll", pass_context=True, invoke_without_command=True) async def cmd_poll(self, ctx, question, *answers): - """ Create poll """ + """ Create a new poll """ await Poll(self.bot, question, list(answers), ctx.author.id).send_poll(ctx) @@ -42,3 +42,7 @@ class Polls(commands.Cog): await poll.delete_poll() else: await poll.close_poll() + + +async def setup(bot: commands.Bot) -> None: + await bot.add_cog(Polls(bot)) diff --git a/requirements.txt b/requirements.txt old mode 100644 new mode 100755 index a8fc1564a2e0f1b8e6d02119c6b21a980e1c2068..8bd5af4977fad55c7e8471ac6b2357e12f1fc806 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,7 @@ aiosignal==1.2.0 async-timeout==4.0.2 attrs==21.4.0 charset-normalizer==2.1.0 -discord.py @ git+https://github.com/Rapptz/discord.py.git@dc50736bfc3340d7b999d9f165808f8dcb8f1a60 +discord.py emoji==2.0.0 frozenlist==1.3.0 idna==3.3 diff --git a/utils.py b/utils.py old mode 100644 new mode 100755 diff --git a/xanathar.py b/xanathar.py old mode 100644 new mode 100755 index f28e9155598f256884f8678d1b6f5aa2b9b3df99..634fd38da0235ee28d2c00cf7cc00d14e026c4f9 --- a/xanathar.py +++ b/xanathar.py @@ -1,4 +1,5 @@ import os +from typing import List import discord from discord.ext import commands @@ -12,14 +13,32 @@ TOKEN = os.getenv('DISCORD_TOKEN') ACTIVITY = os.getenv('DISCORD_ACTIVITY') intents = discord.Intents.all() -bot = commands.Bot(command_prefix='!', help_command=None, activity=discord.Game(ACTIVITY), intents=intents) +help_command = commands.DefaultHelpCommand(dm_help=True) +extensions = ["cogs.appointments", "cogs.polls"] +class Xanathar(commands.Bot): + def __init__( + self, + *args, + initial_extensions: List[str], + **kwargs, + ): + super().__init__(*args, **kwargs) + self.initial_extensions = initial_extensions -@bot.event -async def on_ready(): - print("Client started!") - await bot.add_cog(polls.Polls(bot)) - await bot.add_cog(appointments.Appointments(bot)) + # async def on_ready(self): + # print("Client started!") + async def setup_hook(self) -> None: + for extension in self.initial_extensions: + await self.load_extension(extension) + + guild = discord.Object(id=731078161919377499) + self.tree.copy_global_to(guild=guild) + await self.tree.sync(guild=guild) + + +bot = Xanathar(command_prefix='!', help_command=help_command, activity=discord.Game(ACTIVITY), intents=intents, + initial_extensions=extensions) bot.run(TOKEN)