-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_playback.py
83 lines (73 loc) · 3.07 KB
/
message_playback.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 MessagePlayback(commands.Cog):
def __init__(self, bot):
try:
with open('config/message_playback.json', 'r') as config:
self.messages = json.load(config)
except:
self.messages = {}
@commands.group(aliases=['mp'])
async def message_playback(self, ctx):
if ctx.invoked_subcommand is None:
await ctx.send('Use this command to play back messages!')
@message_playback.command(aliases=['a'])
async def add(self, ctx, *, arg=None):
if arg is None:
await ctx.send('This is not good')
return
space = arg.index(' ')
list_name = arg[:space]
message = arg[space + 1:]
self.add_message(list_name, message)
await ctx.send(f'{ctx.author.mention} I added \n > {message}\n to the list **{list_name}**')
self.save()
@message_playback.command(aliases=['ls', 'l'])
async def list(self, ctx, list_name=None):
if list_name is None:
if len(self.messages) == 0:
await ctx.send('No lists exist')
return
names = '\n'.join(list(self.messages.keys()))
await ctx.send(f'The following lists exist:\n{names}')
else:
if list_name not in self.messages:
await ctx.send('List does not exist')
return
msgs = '\n'.join(self.messages[list_name])
await ctx.send(f'The list **{list_name}** constains:\n{msgs}')
@message_playback.command(aliases=['p'])
async def playback(self, ctx, list_name=None):
if list_name is None:
await ctx.send('This is not good')
if list_name not in self.messages:
await ctx.send('List does not exist')
else:
for message in self.messages[list_name]:
await ctx.send(message)
@message_playback.command(aliases=['rm', 'r'])
async def remove(self, ctx, list_name=None, index=-1):
if list_name is None:
await ctx.send('This is not good')
if list_name not in self.messages:
await ctx.send('List does not exist')
if len(self.messages[list_name]) <= index:
await ctx.send('Index not in list')
if index == -1:
del self.messages[list_name]
await ctx.send(f'Removed the list **{list_name}**.')
self.save()
else:
message = self.messages[list_name][index]
del self.messages[list_name][index]
if len(self.messages[list_name]) == 0:
del self.messages[list_name]
await ctx.send(f'Removed the message: \n> {message}\n from the list **{list_name}**.')
self.save()
def add_message(self, list_name, message):
if list_name not in self.messages:
self.messages[list_name] = []
self.messages[list_name].append(message)
def save(self):
with open('config/message_playback.json', 'w') as config:
config.write(json.dumps(self.messages, indent=2))