-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
110 lines (95 loc) · 3.67 KB
/
bot.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
import discord
import sys
import asyncio
import random
import pymongo
import dateutil.parser
from bank import Bank
from stockwatch import StockWatch
from voicealert import VoiceAlert
from autopublish import AutoPublish
# To RUN in CMD/Powershell/BASH:
# >> python bot.py your-token-here
client = discord.Client()
delimiter = "$"
# to install a new module, put a comma at the end of the bottom module and insert the call to the new module like Module()
# pass in client
modules = [
Bank(client),
StockWatch(client),
VoiceAlert(client),
AutoPublish(client)
]
commands = {}
for mods in modules:
for args in mods.commands.keys(): # for all commands in the module
commands[args] = {"nargs": mods.commands[args][1], "module": modules.index(mods), "help": modules[modules.index(mods)].commands[args][3]} # command, amt args, module it belongs to
def get_error_response():
responses = [
"come again?",
"what",
"?",
"??",
"excuse me",
"uhh"
]
return responses[random.randint(0, len(responses) - 1)]
def get_user_id_from_message(msg):
output = ""
for char in msg:
if char.isnumeric():
output = output + char
#print(output)
return int(output)
def get_help_all():
output = "```"
for mod in modules:
output += "(" + mod.__class__.__name__ + ")\n"
output += "```"
return output
def get_help_module(module):
output = "```"
for cmd in commands:
if modules[commands[cmd]["module"]].__class__.__name__ == module:
output += "(" + modules[commands[cmd]["module"]].__class__.__name__ + ") " + cmd + " | " + commands[cmd]["help"] + "\n"
output += "```"
return output
# https://discordpy.readthedocs.io/en/latest/quickstart.html
@client.event
async def on_ready():
print("Logged in as {0.user}".format(client))
for mods in modules:
await mods.initial_bank_load() # initialize all modules
@client.event
async def on_message(message):
#print(commands)
if message.author == client.user:
return
args = message.content.split()
if args[0].startswith(delimiter):
#print(args[0][1:])
if args[0][1:] == "help" or args[0][1:] == "commands":
try:
if len(args) == 1: # list all commands
await message.channel.send(get_help_all()) #
elif len(args) == 2: # list 1 command
await message.channel.send(get_help_module(args[1]))
else:
await message.channel.send(get_error_response())
except:
await message.channel.send(get_error_response())
#elif args[0][1:] == "test_mongo":
# await message.channel.send("Temperature at " + "1984-03-05T13:00:00.000+00:00\n" + str(mongodb_client.sample_weatherdata.data.find_one({"ts": dateutil.parser.parse("1984-03-05T13:00:00.000+00:00")})["airTemperature"]["value"]))
elif args[0][1:] in commands: # remove delimiter and check
owner_module = commands[args[0][1:]] # index of the owning module
# print("ARGS GIVEN: ", len(args), "COMMAND ARGS: ", owner_module["nargs"])
# print("MODULE: ", owner_module["module"])
if len(args) >= owner_module["nargs"]: # if right number of args
#try:
print("about to try: ", args[0])
await modules[owner_module["module"]].handle_command(args, client, message) # send the whole thing to that module to figure out
#except Exception as e:
#print("error: ", str(e))
#await message.channel.send(get_error_response())
return
client.run(sys.argv[1])