diff --git a/bot/ext/rsudo.py b/bot/ext/rsudo.py index 05f28b7..2a5ebd6 100644 --- a/bot/ext/rsudo.py +++ b/bot/ext/rsudo.py @@ -1,4 +1,5 @@ import logging +import asyncio import discord @@ -7,6 +8,17 @@ from .common import Cog log = logging.getLogger(__name__) +async def shell(command: str): + process = await asyncio.create_subprocess_shell( + command, + stderr=asyncio.subprocess.PIPE, + stdout=asyncio.subprocess.PIPE, + ) + + out, err = map(lambda s: s.decode('utf-8'), await process.communicate()) + return f'{out}{err}'.strip() + + class Rsudo(Cog): def __init__(self, bot): super().__init__(bot) @@ -33,10 +45,25 @@ class Rsudo(Cog): await m.add_reaction('\N{WHITE HEAVY CHECK MARK}') await m.add_reaction('\N{CROSS MARK}') - # wait for the check reaction and - # execute the command + emotes = ['\N{WHITE HEAVY CHECK MARK}', + '\N{CROSS MARK}'] - # TODO: this + def check(reaction, user): + return user and \ + reaction.emoji in emotes and \ + any(x.id == self.bot.config.admin_role for x in user.roles) + + reaction, user = await self.bot.wait_for('reaction_add', check=check) + + if reaction.emoji == emotes[0]: + # do + await self.command_channel.send('executing') + out = await shell(command) + fmt = f'```\n{out}\n```' + await self.command_channel.send(fmt) + else: + # dont do + await self.command_channel.send('denied with success') def setup(bot): diff --git a/config.py b/config.py index b697ccc..7cf41e7 100644 --- a/config.py +++ b/config.py @@ -9,3 +9,5 @@ bot_token = 'Mzg2Mjc1MDc3MzY2NDgwODk4.DQNi9A.BnyE5MnKaIaVMBbWiW9rVDwkrSs' owner_id = 162819866682851329 command_channel = 385653662724259840 + +admin_role = 386566510803681280