-
Notifications
You must be signed in to change notification settings - Fork 1
/
switchboard.py
257 lines (219 loc) · 8.02 KB
/
switchboard.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
from random import choice
import discord
from discord import Embed
from discord import File as DiscordFile
import thltb
import tword
import tquote
import tsched
import tlog
import tstat
# import tcustom
import tgif
import tplex
from tutil import is_admin, config_helper, fetch_file, increment_usage, debug, is_admin_test
# from speller import Speller
from constants import EIGHTBALL, DEFAULT_DIR, help_general, VERBOSE
# Break out functions to prevent dispatch() from
# becoming overly lengthy
async def get_quote(result: dict) -> dict:
"""
Pass off to tquote.py
:param result:
:return:
"""
banner = await tquote.helper(result["message"])
if type(banner) is str:
result["rawText"] = banner
elif type(banner) is list:
result["rawText"] = banner[0]
result["file"] = banner[1]
else:
result["embed"] = banner
return result
async def get_plex(result: dict) -> dict:
"""
Pass off to tplex.py
:param result:
:return:
"""
# TODO add rich embeds
banner = await tplex.helper(result["message"])
result["rawText"] = banner
return result
async def get_hltb(result: dict) -> dict:
"""
Pass off to thltb.py
How Long to Beat dot com search results.
:param result:
:return:
"""
# TODO build this out
# TODO add rich embeds
banner = await thltb.helper(result["message"])
result["rawText"] = banner
return result
async def get_wolfram(result: dict) -> dict:
"""
Pass off to tword.py
:param result:
:return:
"""
banner = await tword.wolfram(result["message"])
if type(banner) is list:
result["rawText"] = banner[0]
result["file"] = banner[1]
else:
result["embed"] = banner
return result
async def get_gif(result: dict) -> dict:
"""
Pass off to tgif.py
:param result:
:return:
"""
args = result["message"].content.split()
if len(args) > 1 and args[1] == 'add':
await tgif.fetch_react(result["message"])
else:
result["file"] = tgif.get_react(result["message"])
return result
async def get_help(result: dict) -> dict:
"""
Return help text.
:param result:
:return:
"""
increment_usage(result["message"].guild, 'help')
help_ = fetch_file('help', 'general')
if not await is_admin(result["message"].author, result["message"]):
help_ = help_.split('For Admins:')[0]
banner = Embed(title='General Help', description=help_)
banner.add_field(name='Help support this bot!', value='All donations go to development and server costs.',
inline=False)
banner.add_field(name='PayPal', value=help_general['paypal'])
# TODO patreon + invite?
# banner.add_field(name='Patreon', value=help_general['patreon'])
banner.add_field(name='More Information',
value='This bot is open source, find it at: {}'.format(help_general['github']))
banner.add_field(name='Invite the bot to your server.', value=help_general['invite'], inline=False)
result["embed"] = banner
return result
async def dispatch(message: discord.Message) -> (None, dict):
"""
Process raw discord.Message object and send it to dedicated function.
:param message:
:return:
"""
# print(message)
# print(message.content)
# Preprocessing
# Prevent recursive loops or triggering from other bots.
if message.author.bot:
return None
# TODO implement this into config
# Confirm we are in an approved channel
# if len(config['discord']['AllowedChannels']) > 0 and str(channel.id) not in config['discord'][
# 'AllowedChannels'].split(','):
# return
# Register a new message in the database
increment_usage(message.guild, 'raw_messages')
await tlog.log_message(message)
# Setup for checking commands
args = message.content.split()
result = {"message": message, "rawText": None, "embed": None, "file": None}
# TODO redo logging
if VERBOSE >= 2:
print('[-] {} by {} in {} - {}'.format(args[0][1:], message.author.name, message.channel.name, message.guild.name))
# Guild level custom commands
# custom = tcustom.get_command(message)
# custom = None
custom = False
if custom:
if VERBOSE >= 2:
print('[-] {} by {} in {} - {}'.format(
'arg: {}'.format(args[0]), message.author.name, message.channel.name, message.guild.name))
result["rawText"] = custom[0]
result["embed"] = custom[1]
return result
# Correct minor typos
# TODO rewrite library defs before using
# print('correcting typos')
# spell = Speller('cmd')
# operator = spell(args[0][1:])
# print(args)
operator = args[0][1:]
# TODO move this config.ini + constants file
if args[0][0] != '!':
return None
if VERBOSE >= 2:
print('[-] {} by {} in {} - {}'.format(operator, message.author.name, message.channel.name, message.guild.name))
# Quotes
if operator in {'quote', 'lore', 'q', 'l'}:
# print('getting quote')
result = await get_quote(result)
# TODO update link
if operator in {'invite'}:
result["rawText"] = "https://discord.com/api/oauth2/authorize?client_id=663696399862595584&permissions=7433793&scope=bot"
return result
# Plex Media Server
if operator in {'plex', 'movie', 'movies'}:
# print('calling plex')
result = await get_plex(result)
# How Long to Beat
if operator in {'hltb'}:
# print('calling hltb')
result = await get_hltb(result)
# Wolfram Alpha
elif operator in {'w', 'wolf', 'wolfram'}:
result = await get_wolfram(result)
# Word of the Day
elif operator in {'word', 'wotd'}:
result["embed"] = await tword.get_todays_word(message)
# Wiktionary
elif operator in {'dict', 'dictionary', 'wiki', 'wiktionary'}:
result["embed"] = tword.wiki(message)
# Config
elif operator == 'config':
result["file"] = await config_helper(message)
if result["file"] and type(result["file"]) is not DiscordFile:
result["file"] = DiscordFile(result["file"])
await message.author.send(file=result["file"])
return None
# Log
elif operator == 'log' and await is_admin_test(message.author, message):
banner = tlog.get_log(message)
result["file"] = banner[1]
if result["file"] and type(result["file"]) is not DiscordFile:
result["file"] = DiscordFile(result["file"])
await message.author.send(content=banner[0], file=result["file"])
return None
# Schedule
elif operator in {'schedule', 'sched', 's'}:
result["embed"] = tsched.helper(message)
elif operator in {'next'}:
result["embed"] = tsched.helper(message, 'next')
# Yandex
elif operator in {'yandex', 'image', 'tineye', 'reverse'}:
result["rawText"] = tword.yandex(message)
# Doip
elif operator == 'doip' and int(message.guild.id) == 453859595262361611:
increment_usage(message.guild, 'doip')
result["rawText"] = tquote.get_quote(message, tquote.Quotes, "LaDoIphin", True)
result["file"] = '{}/docs/doip.jpg'.format(DEFAULT_DIR)
# GIF
elif operator in {'gif', 'react', 'meme'}:
return await get_gif(result)
# Stats
elif operator == 'stats':
result["file"], result["embed"] = tstat.helper(message)
# Magic Eightball
elif operator in {'8ball', '88ball', 'ball', '8', 'eightball', 'eight'}:
increment_usage(message.guild, 'eight')
result["rawText"] = choice(EIGHTBALL)
# General Help
elif operator == 'help':
result = await get_help(result)
if result["file"] and type(result["file"]) is not DiscordFile:
result["file"] = DiscordFile(result["file"])
return result