From ec7bdc6b206152d39e9def7f847e6356712bf926 Mon Sep 17 00:00:00 2001 From: Aaron Riedel Date: Sun, 23 Jan 2022 19:41:22 +0100 Subject: [PATCH] added Slash commands --- bot.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 65 insertions(+), 8 deletions(-) diff --git a/bot.py b/bot.py index 35cbdec..73d07b2 100644 --- a/bot.py +++ b/bot.py @@ -7,6 +7,7 @@ import os from os import system from os import environ from discord.ext import commands +from discord.commands import Option def left(s, amount): return s[:amount] @@ -59,6 +60,61 @@ async def on_command_error(ctx, error): async def on_ready(): print("Bot ready on Version %s..." % discord.__version__) +class Confirm(discord.ui.View): + def __init__(self): + super().__init__() + self.value = None + @discord.ui.button(label="Confirm", style=discord.ButtonStyle.green) + async def confirm( + self, button: discord.ui.Button, interaction: discord.Interaction + ): + await interaction.response.send_message("Bestätigt", ephemeral=True, delete_after=5.0) + self.value = True + self.stop() + @discord.ui.button(label="Cancel", style=discord.ButtonStyle.grey) + async def cancel(self, button: discord.ui.Button, interaction: discord.Interaction): + await interaction.response.send_message("Abgebrochen", ephemeral=True, delete_after=5.0) + self.value = False + self.stop() + + +@bot.slash_command(guild_ids=[261575556708040705]) # create a slash command for the supplied guilds +async def hello(ctx): + """Say hello to the bot""" + await ctx.respond(content="Hallo %s" % ctx.author.display_name, ephemeral=True) + +@bot.slash_command(guild_ids=[261575556708040705]) +async def roll(ctx, + dice: Option(str, "Würfel die du werfen willst. z.B. 4W20"), + ): + await ctx.defer() + rollt = 0 + rolle = " " + roll = dice.lower() + if "d" in roll: + rolls = "d" + if "w" in roll: + rolls = "w" + rollc = roll.split(rolls)[0] + if rollc == "": + rollc = 1 + else: + rollc = int(rollc) + rolld = int(roll.split(rolls)[1]) + for x in range(rollc): + rollo = random.randint(1, rolld) + rollt = rollt + rollo + rolle = rolle + " :game_die: " + str(rollo) + " " + if rollc > 1: + rolltotal = "Total: " + str(rollt) + else: + rolltotal = "" + if rollc > 12: + rolltotal = rolle + "\n" + rolltotal + rolle = "" + em = discord.Embed(title=rolle, description=rolltotal, colour=0x009933) + await ctx.respond(embed=em) + @bot.command(help="Wirft den gleichen Text zurück.", usage="", hidden=True) @is_admin() async def test(ctx, arg): @@ -284,18 +340,19 @@ async def love(ctx, *, arg): async def prune(ctx): await ctx.message.delete() count = await ctx.guild.estimate_pruned_members(days=30) - question = await ctx.channel.send("Sollen wirklich {} Leichen gekickt werden?".format(count)) - def check(reaction, user): - return user == ctx.author and str(reaction.emoji) == "✅" - await question.add_reaction("✅") - try: - reaction, user = await bot.wait_for("reaction_add", timeout=60.0, check=check) - except asyncio.TimeoutError: + view = Confirm() + question = await ctx.send("Sollen wirklich {} Leichen gekickt werden?".format(count), view=view) + await view.wait() + if view.value is None: await question.delete() - else: + await ctx.send(content="Zeit ausgelaufen", delete_after=5.0) + elif view.value: await question.delete() deleted = await ctx.guild.prune_members(days=30) await ctx.send(content='Ich habe {} Leichen beseitigt.'.format(deleted), delete_after=5.0) + else: + await question.delete() + @bot.command(help="Rolle einen oder mehrere Würfel", usage="W (z.B. %sroll W20 oder %sroll 10W6)" % (prefix, prefix)) @is_member()