Merge pull request 'upgrade to py-cord libary 2.0 and adding slash commands' (#2) from pycord-2 into master

Reviewed-on: aaron/shbot#2
This commit is contained in:
Aaron Riedel 2022-01-29 07:47:55 +01:00
commit e7e23bf0f7
2 changed files with 106 additions and 21 deletions

125
bot.py
View file

@ -7,6 +7,8 @@ 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
from discord.commands import permissions
def left(s, amount): def left(s, amount):
return s[:amount] return s[:amount]
@ -22,25 +24,29 @@ prefix = os.environ['PREFIX']
bot = commands.Bot(command_prefix=prefix) bot = commands.Bot(command_prefix=prefix)
client = discord.Client() client = discord.Client()
admin_role=261603488331595776
member_role=261603747711418371
gm_role=511893805956595722
def admin(ctx): def admin(ctx):
if ctx.guild.get_role(261603488331595776) in ctx.author.roles: if ctx.guild.get_role(admin_role) in ctx.author.roles:
return True return True
def member(ctx): def member(ctx):
if ctx.guild.get_role(261603747711418371) in ctx.author.roles: if ctx.guild.get_role(member_role) in ctx.author.roles:
return True return True
def is_admin(): def is_admin():
async def predicate(ctx): async def predicate(ctx):
if ctx.guild.get_role(261603488331595776) in ctx.author.roles: if ctx.guild.get_role(admin_role) in ctx.author.roles:
return True return True
return commands.check(predicate) return commands.check(predicate)
def is_member(): def is_member():
async def predicate(ctx): async def predicate(ctx):
if ctx.guild.get_role(261603747711418371) in ctx.author.roles: if ctx.guild.get_role(member_role) in ctx.author.roles:
return True return True
return commands.check(predicate) return commands.check(predicate)
def is_gm(): def is_gm():
async def predicate(ctx): async def predicate(ctx):
if ctx.guild.get_role(511893805956595722) in ctx.author.roles: if ctx.guild.get_role(gm_role) in ctx.author.roles:
return True return True
return commands.check(predicate) return commands.check(predicate)
@ -59,10 +65,88 @@ 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__)
@bot.command(help="Wirft den gleichen Text zurück.", usage="<Text>", hidden=True) class Confirm(discord.ui.View):
@is_admin() def __init__(self):
async def test(ctx, arg): super().__init__()
await ctx.send(arg) 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])
@permissions.has_role(member_role)
async def roll(ctx,
dice: Option(str, "Würfel den/die du werfen willst. z.B. W20, 3d6", default="W20"),
):
"""Rolle einen oder mehrere Würfel"""
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.response.send_message(embed=em)
@bot.slash_command(guild_ids=[261575556708040705])
@permissions.has_role(gm_role)
async def gmroll(ctx,
dice: Option(str, "Würfel den/die du werfen willst. z.B. W20, 3d6", default="W20"),
):
"""Rolle einen oder mehrere Würfel verdeckt"""
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.response.send_message(embed=em, ephemeral=True)
@bot.command(help="veraltet", usage="", hidden=True) @bot.command(help="veraltet", usage="", hidden=True)
@is_member() @is_member()
@ -95,7 +179,7 @@ async def survey(ctx, *, arg):
desc = desc + emojinumbers[z] + " - " + y + "\n" desc = desc + emojinumbers[z] + " - " + y + "\n"
z = z + 1 z = z + 1
em = discord.Embed(title=question, description=desc, colour=0x00E0FF) em = discord.Embed(title=question, description=desc, colour=0x00E0FF)
em.set_author(name=ctx.author.display_name, url=discord.Embed.Empty, icon_url=ctx.author.avatar_url) em.set_author(name=ctx.author.display_name, url=discord.Embed.Empty, icon_url=ctx.author.avatar.url)
ask_msg = await ctx.send(content="||@here||",embed=em) ask_msg = await ctx.send(content="||@here||",embed=em)
a = 0 a = 0
for x in emojinumbers: for x in emojinumbers:
@ -119,7 +203,7 @@ async def surveyedit(ctx, *, arg):
desc = desc + emojinumbers[z] + " - " + y + "\n" desc = desc + emojinumbers[z] + " - " + y + "\n"
z = z + 1 z = z + 1
em = discord.Embed(title=question, description=desc, colour=0x00E0FF) em = discord.Embed(title=question, description=desc, colour=0x00E0FF)
em.set_author(name=ctx.author.display_name, url=discord.Embed.Empty, icon_url=ctx.author.avatar_url) em.set_author(name=ctx.author.display_name, url=discord.Embed.Empty, icon_url=ctx.author.avatar.url)
survey_msg = await ctx.channel.fetch_message((int(survey_id))) survey_msg = await ctx.channel.fetch_message((int(survey_id)))
await survey_msg.edit(embed=em) await survey_msg.edit(embed=em)
@ -128,7 +212,7 @@ async def surveyedit(ctx, *, arg):
async def vote(ctx, *, arg): async def vote(ctx, *, arg):
await ctx.message.delete() await ctx.message.delete()
em = discord.Embed(description=arg, colour=0x00E0FF) em = discord.Embed(description=arg, colour=0x00E0FF)
em.set_author(name=ctx.author.display_name, url=discord.Embed.Empty, icon_url=ctx.author.avatar_url) em.set_author(name=ctx.author.display_name, url=discord.Embed.Empty, icon_url=ctx.author.avatar.url)
ask_msg = await ctx.send(content="||@here||",embed=em) ask_msg = await ctx.send(content="||@here||",embed=em)
for x in ["", "", ""]: for x in ["", "", ""]:
await ask_msg.add_reaction(x) await ask_msg.add_reaction(x)
@ -284,18 +368,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()

View file

@ -1 +1 @@
discord.py py-cord==2.0.0b1