-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspam.py
66 lines (53 loc) · 2.23 KB
/
spam.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
# spam.py: Manage server spam channel bindings, implement sending messages to spam channel on all servers.
import asyncio
import chat
import discord
import dbm
import os
import os.path
_servermap = dbm.open(os.path.join(os.environ["HOME"], "r99infospam.db"), "c")
async def init():
asyncio.ensure_future(_sync_db())
async def _sync_db():
global _servermap
while True:
_servermap.close()
_servermap = dbm.open(os.path.join(os.environ["HOME"], "r99infospam.db"), "c")
await asyncio.sleep(5)
_SPAM_CATEGORY = "Spam management"
@chat.command("!bind", _SPAM_CATEGORY)
async def bind(msg):
"""(admin) Have the bot place all of its messages in the channel the command was sent in"""
if msg.author.server_permissions.administrator:
_servermap[msg.server.id] = msg.channel.name
chat.fade(await chat.send_info(msg.channel, "`bind`", "Sending spam to this channel"))
else:
chat.fade(await chat.send_error(msg.channel, "`bind`", "You are not a server administrator, "+msg.author.mention))
@chat.command("!unbind", _SPAM_CATEGORY)
async def unbind(msg):
"""(admin) Request that the bot not send any further spam to this server."""
if msg.author.server_permissions.administrator:
if msg.server.id in _servermap:
del _servermap[msg.server.id]
chat.fade(await chat.send_info(msg.channel, "`unbind`", "No longer sending bot spam to this server"))
else:
chat.fade(await chat.send_error(msg.channel, "`unbind`", "You are not a server administrator, "+msg.author.mention))
async def send(*args, **kwargs):
"""Send a message/embed/etc to all spam channels."""
dead_ids = []
for idbytes in _servermap.keys():
server_id = str(idbytes, "utf8")
channame = str(_servermap[server_id], "utf8")
server = chat.get_server(server_id)
if server is None:
dead_ids.append(server_id)
continue
for channel in server.channels:
if channel.name == channame:
await chat.send_message(channel, *args, **kwargs)
break
else:
dead_ids.append(server_id)
for server_id in dead_ids:
print("deleting "+server_id)
del _servermap[server_id]