added Slash commands

This commit is contained in:
Aaron Riedel 2022-01-23 19:41:22 +01:00
parent 86fd0048c1
commit ec7bdc6b20

73
bot.py
View file

@ -7,6 +7,7 @@ import os
from os import system from os import system
from os import environ from os import environ
from discord.ext import commands from discord.ext import commands
from discord.commands import Option
def left(s, amount): def left(s, amount):
return s[:amount] return s[:amount]
@ -59,6 +60,61 @@ async def on_command_error(ctx, error):
async def on_ready(): async def on_ready():
print("Bot ready on Version %s..." % discord.__version__) 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="<Text>", hidden=True) @bot.command(help="Wirft den gleichen Text zurück.", usage="<Text>", hidden=True)
@is_admin() @is_admin()
async def test(ctx, arg): async def test(ctx, arg):
@ -284,18 +340,19 @@ async def love(ctx, *, arg):
async def prune(ctx): async def prune(ctx):
await ctx.message.delete() await ctx.message.delete()
count = await ctx.guild.estimate_pruned_members(days=30) count = await ctx.guild.estimate_pruned_members(days=30)
question = await ctx.channel.send("Sollen wirklich {} Leichen gekickt werden?".format(count)) view = Confirm()
def check(reaction, user): question = await ctx.send("Sollen wirklich {} Leichen gekickt werden?".format(count), view=view)
return user == ctx.author and str(reaction.emoji) == "" await view.wait()
await question.add_reaction("") if view.value is None:
try:
reaction, user = await bot.wait_for("reaction_add", timeout=60.0, check=check)
except asyncio.TimeoutError:
await question.delete() await question.delete()
else: await ctx.send(content="Zeit ausgelaufen", delete_after=5.0)
elif view.value:
await question.delete() await question.delete()
deleted = await ctx.guild.prune_members(days=30) deleted = await ctx.guild.prune_members(days=30)
await ctx.send(content='Ich habe {} Leichen beseitigt.'.format(deleted), delete_after=5.0) 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="<anzahl_optional>W<seitenzahl> (z.B. %sroll W20 oder %sroll 10W6)" % (prefix, prefix)) @bot.command(help="Rolle einen oder mehrere Würfel", usage="<anzahl_optional>W<seitenzahl> (z.B. %sroll W20 oder %sroll 10W6)" % (prefix, prefix))
@is_member() @is_member()