memed/bot/ext/rsudo.py

71 lines
2.0 KiB
Python
Raw Normal View History

2017-12-02 14:24:55 +00:00
import logging
2017-12-02 17:40:19 +00:00
import asyncio
2017-12-02 14:24:55 +00:00
import discord
from .common import Cog
log = logging.getLogger(__name__)
2017-12-02 17:40:19 +00:00
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()
2017-12-02 14:24:55 +00:00
class Rsudo(Cog):
def __init__(self, bot):
super().__init__(bot)
2017-12-02 15:53:47 +00:00
self.command_channel = None
2017-12-02 14:24:55 +00:00
async def create_request(self, message):
2017-12-02 15:53:47 +00:00
if not self.command_channel:
self.command_channel = self.bot.get_channel(
self.bot.config.command_channel)
if not self.command_channel:
log.warning('command channel not found')
return
2017-12-02 14:24:55 +00:00
# parse it, follows format command,uid
uid = message.split(',')[-1]
command = ','.join(message.split(',')[:-1])
log.info(f'[rsudo] {uid!r} {command!r}')
e = discord.Embed(title=f'rsudo from uid {uid}')
e.add_field(name='command', value=f'`{command}`')
2017-12-02 15:53:47 +00:00
m = await self.command_channel.send(embed=e)
await m.add_reaction('\N{WHITE HEAVY CHECK MARK}')
await m.add_reaction('\N{CROSS MARK}')
2017-12-02 14:24:55 +00:00
2017-12-02 17:40:19 +00:00
emotes = ['\N{WHITE HEAVY CHECK MARK}',
'\N{CROSS MARK}']
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)
2017-12-02 15:54:14 +00:00
2017-12-02 17:40:19 +00:00
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')
2017-12-02 15:54:14 +00:00
2017-12-02 14:24:55 +00:00
def setup(bot):
bot.add_cog(Rsudo(bot))