forked from tangotrail/catsite-stork-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortcuts.py
83 lines (72 loc) · 3.18 KB
/
shortcuts.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
79
80
81
82
83
import json
from discord.ext import commands
class Shortcuts(commands.Cog):
def __init__(self, bot):
self.bot = bot
with open('settings.json', 'r') as f:
content = json.load(f)
self.color = int(content['color'][2:], base=16)
self.file = 'shortcuts.json'
# edit shortcuts
@commands.group(invoke_without_subcommand=True, aliases=['sc'])
async def shortcut(self, ctx):
if ctx.invoked_subcommand is None:
cmds = [['/sc add <name> <contents>', 'create a shortcut (name cannot have spaces)'],
['/sc remove <name>', 'remove a shortcut'], ['/sc list', 'list all Shortcuts']]
out = '\n'.join([f"**{cmd[0]}**\n-- {cmd[1]}" for cmd in cmds])
out = 'Summon a shortcut with any of: /get /grab /show /lookup\n\n' + out
await ctx.send(out)
@commands.command(aliases=['get', 'grab', 'show', 'lookup'])
async def shortcut_summon(self, ctx, name=''):
if not name:
with open(self.file, 'r') as f:
content = json.load(f)
if content:
await ctx.send('Summon a shortcut with any of: /get /grab /show /lookup\n**Available Shortcuts:**\n'
+ ', '.join(sorted([x for x in content])))
return
with open(self.file, 'r') as f:
f = json.load(f)
if name not in f:
await ctx.send('Shortcut not found.')
return
await ctx.send(f[name])
@shortcut.command(aliases=['add'])
async def shortcut_add(self, ctx, name, *, msg):
with open(self.file, 'r') as f:
content = json.load(f)
if name in content:
deleted = content[name]
await ctx.send(f"Overrode shortcut **{name}**.")
await ctx.send(f"Old: \n>>> {deleted}")
await ctx.send(f"New: \n>>> {msg}")
else:
await ctx.send(f"Created shortcut **{name}**.")
await ctx.send(f">>> {msg}")
content[name] = msg
with open(self.file, 'w') as f:
json.dump(content, f, indent=4)
@shortcut.command(aliases=['remove'])
async def shortcut_remove(self, ctx, name):
with open(self.file, 'r') as f:
content = json.load(f)
if name in content:
deleted = content[name]
del content[name]
await ctx.send(f"Deleted shortcut **{name}**.")
await ctx.send(f">>> {deleted}")
else:
await ctx.send(f"Shortcut **{name}** does not exist.")
return
with open(self.file, 'w') as f:
json.dump(content, f, indent=4)
@shortcut.command(aliases=['list', 'all'])
async def shortcut_list(self, ctx):
with open(self.file, 'r') as f:
content = json.load(f)
if content:
await ctx.send(', '.join([x for x in content]))
else:
await ctx.send('There are no shortcuts.')
async def setup(bot):
await bot.add_cog(Shortcuts(bot))