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 environ
from discord.ext import commands
from discord.commands import Option
from discord.commands import permissions
def left(s, amount):
return s[:amount]
@ -22,25 +24,29 @@ prefix = os.environ['PREFIX']
bot = commands.Bot(command_prefix=prefix)
client = discord.Client()
admin_role=261603488331595776
member_role=261603747711418371
gm_role=511893805956595722
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
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
def is_admin():
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 commands.check(predicate)
def is_member():
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 commands.check(predicate)
def is_gm():
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 commands.check(predicate)
@ -59,10 +65,88 @@ async def on_command_error(ctx, error):
async def on_ready():
print("Bot ready on Version %s..." % discord.__version__)
@bot.command(help="Wirft den gleichen Text zurück.", usage="<Text>", hidden=True)
@is_admin()
async def test(ctx, arg):
await ctx.send(arg)
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])
@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)
@is_member()
@ -95,7 +179,7 @@ async def survey(ctx, *, arg):
desc = desc + emojinumbers[z] + " - " + y + "\n"
z = z + 1
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)
a = 0
for x in emojinumbers:
@ -119,7 +203,7 @@ async def surveyedit(ctx, *, arg):
desc = desc + emojinumbers[z] + " - " + y + "\n"
z = z + 1
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)))
await survey_msg.edit(embed=em)
@ -128,7 +212,7 @@ async def surveyedit(ctx, *, arg):
async def vote(ctx, *, arg):
await ctx.message.delete()
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)
for x in ["", "", ""]:
await ask_msg.add_reaction(x)
@ -284,18 +368,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="<anzahl_optional>W<seitenzahl> (z.B. %sroll W20 oder %sroll 10W6)" % (prefix, prefix))
@is_member()

View file

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