Basic command functionality

This commit is contained in:
Ske 2018-10-27 22:12:10 +02:00
parent 4217d5d5d8
commit ea62ede21b

View File

@ -1,3 +1,4 @@
import asyncio
import discord import discord
import logging import logging
import re import re
@ -69,7 +70,7 @@ class CommandContext:
def has_next(self) -> bool: def has_next(self) -> bool:
return bool(self.args) return bool(self.args)
def pop_str(self, error: CommandError = None) -> str: def pop_str(self, error: CommandError = None) -> Optional[str]:
if not self.args: if not self.args:
if error: if error:
raise error raise error
@ -105,26 +106,27 @@ class CommandContext:
return self.args return self.args
async def reply(self, content=None, embed=None): async def reply(self, content=None, embed=None):
return await self.client.send_message(self.message.channel, content=content, embed=embed) return await self.message.channel.send(content=content, embed=embed)
async def confirm_react(self, user: Union[discord.Member, discord.User], message: str): async def confirm_react(self, user: Union[discord.Member, discord.User], message: str):
message = await self.reply(message) message = await self.reply(message)
await message.add_reaction("\u2705") # Checkmark
await message.add_reaction("\u274c") # Red X
await self.client.add_reaction(message, "") try:
await self.client.add_reaction(message, "") reaction, _ = await self.client.wait_for("reaction_add", check=lambda r, u: u.id == user.id and r.emoji in ["\u2705", "\u274c"], timeout=60.0*5)
return reaction.emoji == "\u2705"
reaction = await self.client.wait_for_reaction(emoji=["", ""], user=user, timeout=60.0*5) except asyncio.TimeoutError:
if not reaction:
raise CommandError("Timed out - try again.") raise CommandError("Timed out - try again.")
return reaction.reaction.emoji == ""
async def confirm_text(self, user: discord.Member, channel: discord.TextChannel, confirm_text: str, message: str): async def confirm_text(self, user: discord.Member, channel: discord.TextChannel, confirm_text: str, message: str):
await self.reply(message) await self.reply(message)
message = await self.client.wait_for_message(channel=channel, author=user, timeout=60.0*5) try:
if not message: message = await self.client.wait_for("message", check=lambda m: m.channel.id == channel.id and m.author.id == user.id, timeout=60.0*5)
return message.content.lower() == confirm_text.lower()
except asyncio.TimeoutError:
raise CommandError("Timed out - try again.") raise CommandError("Timed out - try again.")
return message.content == confirm_text
import pluralkit.bot.commands.import_commands import pluralkit.bot.commands.import_commands