-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcogs_batchcmds.py
78 lines (62 loc) · 2.47 KB
/
cogs_batchcmds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# The MetaCommand Cog, which handles all batch commands.
from pathlib import Path
import discord as dc
from discord.ext import commands
from cogs_textbanks import url_bank, query_bank, response_bank
from bot_common import bot, bot_coglist, CONST_AUTHOR, user_or_perms
_CMD_DIR = Path('cmd')
class BatchCommands(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_ready(self):
print(response_bank.batch_cog_ready)
@commands.group(name='batch')
@commands.bot_has_permissions(send_messages=True)
@user_or_perms(CONST_AUTHOR, administrator=True)
async def batch(self, ctx):
if ctx.invoked_subcommand is None:
await ctx.send(response_bank.batch_usage_format)
@batch.error
async def batch_error(self, ctx, error):
if isinstance(error, commands.BotMissingPermissions):
return
raise error
@batch.command(name='save')
async def batch_save(self, ctx, name):
if not (name.isascii() and name.replace('_', '').isalnum()):
await ctx.send(response_bank.batch_save_name_error)
return
if not (atts := ctx.message.attachments):
await ctx.send(response_bank.batch_save_missing_file)
return
if len(atts) > 1:
await ctx.send(response_bank.batch_save_ambiguous_file)
return
await atts[0].save(_CMD_DIR / f'{name}.txt')
await ctx.send(response_bank.batch_save_confirm.format(name=name))
@batch_save.error
async def batch_save_error(self, ctx, error):
raise error
@batch.command(name='exec')
async def batch_exec(self, ctx, name):
if not (fp := _CMD_DIR / f'{name}.txt').exists():
await ctx.send(response_bank.batch_exec_name_error.format(name=name))
return
await ctx.send(response_bank.batch_exec_start.format(name=name))
with fp.open('r') as cmdfile:
msg = ctx.message
msg.author = bot.user
for line in cmdfile:
msg.content = line.strip()
try:
await self.bot.process_commands(msg)
except Exception as exc:
await ctx.send(f'{type(exc).__name__}: {"".join(exc.args)}')
raise
@batch_exec.error
async def batch_exec_error(self, ctx, error):
raise error
async def setup():
await bot.add_cog(BatchCommands(bot))
bot_coglist.append(setup())