-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.py
96 lines (77 loc) · 2.39 KB
/
helpers.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
84
85
86
87
88
89
90
91
92
93
94
95
96
# Maki
# ----
# Discord bot by MrDetonia
#
# Copyright 2018 Zac Herd
# Licensed under BSD 3-clause License, see LICENSE.md for more info
# IMPORTS
import asyncio
import discord
import logging
import datetime
import re
# LOCAL IMPORTS
from common import *
# clamps an integer
def clamp(n, small, large):
return max(small, min(n, large))
# converts a datetime to a string
def strfromdt(dt):
return dt.strftime('%Y-%m-%d %H:%M:%S')
# converts a timestamp to a datetime
def dtfromts(ts):
return datetime.datetime.fromtimestamp(ts)
# logging setup
def logger():
logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(
filename='discord.log', encoding='utf-8', mode='w')
handler.setFormatter(
logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
# fuzzy search over dictionary
def search(dict, term):
s = term.lower()
for item in dict.items():
if s in item[0].lower():
yield item
# send_message wrapper (deals with Discord's shit API)
@asyncio.coroutine
def discord_send(client, message, response):
if response is not '' and message.server.id not in quiet:
for attempt in range(5):
try:
yield from client.send_message(message.channel, response)
except discord.errors.HTTPException:
continue
else:
break
else:
print('ERROR: Failed to send message to discord after 5 attempts')
# send typing signal to Discord
@asyncio.coroutine
def discord_typing(client, message):
for attempt in range(5):
try:
yield from client.send_typing(message.channel)
except discord.errors.HTTPException:
continue
else:
break
else:
print(
'ERROR: Failed to send typing signal to discord after 5 attempts')
# Maki Reacts to...
@asyncio.coroutine
def makireacts(client, msg):
# TODO: track down the person(s) responsible for naming emoji
reactions = {
r"\bmaki\b": "\N{BLACK HEART SUIT}",
r"\bbutter\b": "\N{PERSON WITH FOLDED HANDS}",
r"\begg[s]?\b": "\N{AUBERGINE}",
r"\bproblematic\b": "\N{EYEGLASSES}",
}
for i in reactions:
if bool(re.search(i, msg.content, re.IGNORECASE)):
yield from client.add_reaction(msg, reactions[i])