Skip to content

Commit

Permalink
Add confirmation upon q!deny/approve (fixes #5)
Browse files Browse the repository at this point in the history
  • Loading branch information
slice committed May 15, 2018
1 parent e3a81fc commit fd53686
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
9 changes: 5 additions & 4 deletions queuebot/bot.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# -*- coding: utf-8 -*-
import datetime
import importlib
import inspect
import logging
import typing
from pathlib import Path

import datetime

import aiohttp
from asyncpg.pool import Pool
import discord
from asyncpg.pool import Pool
from discord.ext import commands

from queuebot.cog import Cog
from queuebot.context import Context

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -73,7 +73,8 @@ async def on_message(self, msg: discord.Message):
# Do not process commands until we are ready.
await self.wait_until_ready()

await self.process_commands(msg)
context = await self.get_context(msg, cls=Context)
await self.invoke(context)

def load_extension(self, name: str):
extension_module = importlib.import_module(name)
Expand Down
11 changes: 11 additions & 0 deletions queuebot/cogs/queue/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ async def buffer_info(self, ctx):
@is_council()
async def approve(self, ctx, suggestion: Suggestion, *, reason=None):
"""Moves a suggestion from the council queue to the public queue."""
embed = discord.Embed()
embed.set_image(url=suggestion.emoji_url)
if not await ctx.confirm('Are you sure that you want to **approve** this suggestion?', embed=embed,
color=discord.Color.green()):
return

logger.info('%s: Moving %s to public (approval) queue.', ctx.author, suggestion)
reason = reason or None # do not push empty strings
await suggestion.move_to_public_queue(who=ctx.author.id, reason=reason)
Expand All @@ -246,6 +252,11 @@ async def approve(self, ctx, suggestion: Suggestion, *, reason=None):
@is_council()
async def deny(self, ctx, suggestion: Suggestion, *, reason=None):
"""Denies an emoji that is currently in the council queue."""
embed = discord.Embed()
embed.set_image(url=suggestion.emoji_url)
if not await ctx.confirm('Are you sure you want to **deny** this suggestion?', embed=embed):
return

logger.info('%s: Denying %s.', ctx.author, suggestion)
reason = reason or None # do not push empty strings
await suggestion.deny(who=ctx.author.id, reason=reason)
Expand Down
33 changes: 33 additions & 0 deletions queuebot/context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import discord
from discord.ext import commands


class Context(commands.Context):
async def confirm(self, description=None, *, embed=None, color=None):
decision_emojis = [
self.bot.get_emoji(self.bot.config.approve_emoji_id),
self.bot.get_emoji(self.bot.config.deny_emoji_id)
]
embed = embed or discord.Embed()
embed.color = color or discord.Colour.red()
embed.title = 'Are you sure?'
embed.set_footer(text=str(self.author), icon_url=self.author.avatar_url)
embed.description = description
message = await self.send(embed=embed)
for emoji in decision_emojis:
await message.add_reaction(emoji)

def check(reaction, user):
return user == self.author and \
isinstance(reaction.emoji, discord.Emoji) and \
reaction.emoji in decision_emojis

try:
reaction, user = await self.bot.wait_for('reaction_add', check=check, timeout=60.0)
except TimeoutError:
await self.send('Timed out.')
return
result = reaction.emoji == decision_emojis[0]
if not result:
await self.send('Cancelled.')
return result

0 comments on commit fd53686

Please sign in to comment.