1
0
Fork 0

Switch poll to slash commands

This commit is contained in:
Ethanell 2021-02-03 22:38:06 +01:00
parent a2afff2334
commit f82ae0bb3b

View file

@ -1,12 +1,16 @@
import shlex
from datetime import datetime from datetime import datetime
from discord.abc import GuildChannel from discord.abc import GuildChannel
from discord.ext import commands from discord.ext import commands
from discord import Embed, RawReactionActionEvent, RawMessageDeleteEvent, RawBulkMessageDeleteEvent, TextChannel, Guild from discord import Embed, RawReactionActionEvent, RawMessageDeleteEvent, RawBulkMessageDeleteEvent, TextChannel, Guild
from discord.ext.commands import BadArgument from discord.ext.commands import BadArgument
from discord_slash import cog_ext, SlashCommandOptionType, SlashContext
from discord_slash.utils import manage_commands
import db import db
from administrator.check import is_enabled from administrator import slash
from administrator.check import is_enabled, guild_only
from administrator.logger import logger from administrator.logger import logger
from administrator.utils import event_is_enabled from administrator.utils import event_is_enabled
@ -21,47 +25,41 @@ REACTIONS.append("\U0001F51F")
class Poll(commands.Cog): class Poll(commands.Cog):
def __init__(self, bot: commands.Bot): def __init__(self, bot: commands.Bot):
self.bot = bot self.bot = bot
slash.get_cog_commands(self)
def description(self): def description(self):
return "Create poll with a simple command" return "Create poll with a simple command"
@commands.group("poll", pass_context=True) @cog_ext.cog_slash(name="poll",
description="Create a poll",
options=[
manage_commands.create_option("name", "Poll name",
SlashCommandOptionType.STRING, True),
manage_commands.create_option("choices", "All pool choice",
SlashCommandOptionType.STRING, True),
manage_commands.create_option("multi", "Allow multiple choice",
SlashCommandOptionType.BOOLEAN, False)
])
@is_enabled() @is_enabled()
@commands.guild_only() @guild_only()
async def poll(self, ctx: commands.Context, name: str, *choices): async def poll(self, ctx: SlashContext, name: str, choices: str, multi: bool = False):
if name == "help": choices = shlex.split(choices)
await ctx.invoke(self.poll_help) if len(choices) > 11:
raise BadArgument()
else: else:
multi = False embed = Embed(title=f"Poll: {name}")
if choices and choices[0] in ["multi", "m"]: embed.set_author(name=str(ctx.author), icon_url=ctx.author.avatar_url)
multi = True embed.set_footer(text=f"Created: {datetime.now().strftime('%d/%m/%Y %H:%M')}")
choices = choices[1:] for i, choice in enumerate(choices):
if len(choices) == 0 or len(choices) > 11: embed.add_field(name=REACTIONS[i], value=choice, inline=False)
raise BadArgument() message = await ctx.channel.send(embed=embed)
else: reactions = REACTIONS[0:len(choices)] + ["\U0001F5D1"]
embed = Embed(title=f"Poll: {name}") for reaction in reactions:
embed.set_author(name=str(ctx.author), icon_url=ctx.author.avatar_url) await message.add_reaction(reaction)
embed.set_footer(text=f"Created: {ctx.message.created_at.strftime('%d/%m/%Y %H:%M')}") s = db.Session()
for i, choice in enumerate(choices): s.add(db.Polls(message.id, ctx.channel.id, ctx.guild.id, ctx.author.id, reactions, multi))
embed.add_field(name=REACTIONS[i], value=choice, inline=False) s.commit()
message = await ctx.send(embed=embed) s.close()
reactions = REACTIONS[0:len(choices)] + ["\U0001F5D1"]
for reaction in reactions:
await message.add_reaction(reaction)
s = db.Session()
s.add(db.Polls(message.id, ctx.channel.id, ctx.guild.id, ctx.message.author.id, reactions, multi))
s.commit()
s.close()
await ctx.message.delete()
@poll.group("help", pass_context=True)
async def poll_help(self, ctx: commands.Context):
embed = Embed(title="Poll help")
embed.add_field(name="poll <name> [multi|m] <Choice N°1> <Choice N°2> ... <Choice N°11>",
value="Create a poll, the argument multi (or m) after the name allow multiple response\n"
"User the \U0001F5D1 to close the poll",
inline=False)
await ctx.send(embed=embed)
@commands.Cog.listener() @commands.Cog.listener()
async def on_raw_reaction_add(self, payload: RawReactionActionEvent): async def on_raw_reaction_add(self, payload: RawReactionActionEvent):