-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot_command.py
144 lines (118 loc) · 4.28 KB
/
bot_command.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
import discord
import asyncio
import command_list as cmdlst
import sys
import importlib
import os
from enum import Enum
from bot_refuse import refuse
client = None
class cmd_lvl(Enum):
everyone = 1
#right now we use the mute_users permission to determine mod, but this may not be permanent
mods = 2
admins = 3
bot_admins = 4
class bot_cmd:
def __init__(self, cmd, run, lvl = cmd_lvl.everyone, desc = 'Default Description'):
self.cmd = cmd.lower()
self.run = run
self.desc = desc
self.lvl = lvl
if type(self.lvl) == type(1) :
self.lvl = cmd_lvl(self.lvl)
def __str__(self):
return '{} {}'.format(self.cmd, self.desc)
async def execute(self, message, args, author, client):
if not self.has_perms(author) :
return
#special check here to see if we need to reload the commands
#this only reloads functionality, to add more commands the entire bot must be restarted for now
if(self.cmd == 'reload') :
await bot_cmd.reload(message.channel)
return
#only admins may change refusalLevel of bot
if self.cmd == 'refusallevel' and len(args) :
if str(author.id) in client.bot_admins :
await self.run(message, args, author, client)
return
#if a peasant-level command is executed, the bot might not comply.
if self.lvl == cmd_lvl.everyone and refuse.think_about_refusing() :
await refuse.send_refusal(message, args, author, client)
return
#else run given command
await self.run(message, args, author, client)
@staticmethod
def check_perms(user, lvl) :
if lvl == cmd_lvl.everyone :
return True
if lvl == cmd_lvl.mods :
return type(user) is discord.member.Member and user.top_role.permissions.mute_members
if lvl == cmd_lvl.admins :
return type(user) is discord.member.Member and user.top_role.permissions.administrator
if lvl == cmd_lvl.bot_admins :
return str(user.id) in client.bot_admins
def has_perms(self, user) :
return bot_cmd.check_perms(user, self.lvl)
@staticmethod
async def reload(channel):
global cmdlst
cmdlst = importlib.reload(cmdlst)
client.cmd_list = init_commands(client)
print ('Reloaded commands')
await channel.send('Reloaded command functionality')
def find_command(command, client):
prefix = client.settings.get_val('cmd_prefix')
if (not command.startswith(prefix)):
return None
command = command[len(prefix):]
test = command.lower()
for cmd in client.cmd_list :
if test == cmd.cmd :
return cmd
return None
def parse_command(message):
tmp = message.content.split(' ')
return (tmp[0], [x for x in tmp[1:] if x != ''])
_loaded_modules = {}
def load_plugins():
global _loaded_modules
#print (_loaded_modules)
new_modules = {}
importlib.invalidate_caches()
sys.path.append(sys.path[0] + '/plugins')
#print(sys.path)
index = 0
path = sys.path[0] + '/plugins'
files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
def recurse_obj(obj):
lst = []
if (isinstance(obj, bot_cmd)):
return [obj]
elif (isinstance(obj, list)):
for x in obj:
lst += recurse_obj(x)
return lst
for f in files:
if f in _loaded_modules.keys() :
module = importlib.reload(_loaded_modules[f][0])
else :
module = __import__(f[0:-3])
commands = []
for x in dir(module):
obj = getattr(module, x)
commands += recurse_obj(obj)
new_modules['%s' % f] = (module, commands)
_loaded_modules = new_modules
return _loaded_modules
def init_commands(client):
cmds = []
cmds.append(bot_cmd("reload", None, cmd_lvl.bot_admins, 'Reload the functionality of the commands'))
cmds.append(bot_cmd("rock", cmdlst.rockfact, 1, 'Say a random rock fact!'))
client.markov_chains = None
client.named_markov_chains = None
modules = load_plugins()
for m in modules.keys():
_m, _cmds = modules[m]
cmds += _cmds
return cmds