-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommands.py
214 lines (167 loc) · 8.63 KB
/
commands.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
"""Defines commands.
Do not use without Void's permission
"""
import logging
import sys
from datetime import datetime
from wiki.api import ApiError, ConnectionError
from wiki.helpers import Logger
from command import Command, CommandHandler
logs = logging.getLogger(__name__)
def kill(bot, event):
"""Shutdown bot instance."""
sender = event.source.nick
bot.connection.disconnect(f'{sender} has brought the end upon us!') # TODO cycle quit messages
logs.warning(f'{sender} has killed the bot')
sys.exit(0)
help_str = 'End the bot. (Requires Trusted)'
CommandHandler.master_commands.append(Command('kill', kill, restriction=Command.TRUSTED, help=help_str))
def cmd_disable(bot, event):
"""Disable a command."""
target = event.target if event.type == 'pubmsg' else event.source.nick
args = event.arguments[0].split()[1:]
if len(args) == 0:
return bot.connection.privmsg(target, "I can't disable what you don't tell me about.")
if not CommandHandler.disable_command(args[0]):
return bot.connection.privmsg(target, f'I can\'t disable what does not exist! (Could not find "{args[0]}")')
else:
logs.info(f'{event.source.nick} has disabled command {args[0]}')
help_str = 'Attempt to disable the supplied command. Core commands cannot be disabled. (Requires Trusted)'
CommandHandler.master_commands.append(Command('disable', cmd_disable, restriction=Command.TRUSTED, help=help_str))
def cmd_enable(bot, event):
"""Enable a command."""
target = event.target if event.type == 'pubmsg' else event.source.nick
args = event.arguments[0].split()[1:]
if len(args) == 0:
return bot.connection.privmsg(target, "I can't enable what you don't tell me about.")
if not CommandHandler.enable_command(args[0]):
return bot.connection.privmsg(target, f'I can\'t enable what does not exist! (Could not find "{args[0]}")')
else:
logs.info(f'{event.source.nick} has enabled command {args[0]}')
help_str = 'Attempt to enable the supplied command. (Requires Trusted)'
CommandHandler.master_commands.append(Command('enable', cmd_enable, restriction=Command.TRUSTED, help=help_str))
def nick(bot, event):
"""Change the bot's nick."""
target = event.target if event.type == 'pubmsg' else event.source.nick
args = event.arguments[0].split()[1:]
if len(args) == 0:
return bot.connection.privmsg(target, "That's not a nick! That's nothing!")
bot.connection.nick(args[0])
logs.info(f'{event.source.nick} has changed our nick to {args[0]}')
help_str = 'Change the nick of the bot to the supplied value. (Requires Trusted)'
CommandHandler.commands.append(Command('nick', nick, restriction=Command.TRUSTED, help=help_str))
def join(bot, event):
"""Join channels."""
target = event.target if event.type == 'pubmsg' else event.source.nick
args = event.arguments[0].split()[1:]
if len(args) == 0:
return bot.connection.privmsg(target, "I can't join if you don't tell me the channel name!")
if args[0][0] != '#':
return bot.connection.privmsg(target, f'"{args[0]}" is not a channel!')
bot.connection.join(args[0])
logs.info(f'{event.source.nick} has asked us to join {args[0]}')
help_str = 'Join the supplied channel. (Requires Trusted)'
CommandHandler.commands.append(Command('join', join, restriction=Command.TRUSTED, help=help_str))
def part(bot, event):
"""Part channels."""
if event.type == 'privmsg':
return bot.connection.privmsg(event.source.nick, 'This command can only be used from a channel!')
channel = event.target
sender = event.source.nick
if channel == '##voidwalker':
return bot.connection.privmsg(channel, 'I cannot leave this channel. Voidwalker hath forbidden it.')
bot.connection.privmsg(channel, f'Goodbye {channel}!')
bot.connection.part(channel, message=f':{sender} has sent me to a far off land!')
logs.info(f'{sender} has caused us to part from {channel}')
help_str = 'Part the current channel. (Requires ChanOp)'
CommandHandler.commands.append(Command('part', part, restriction=Command.OPERATOR, help=help_str))
def partf(bot, event):
"""Part from channels."""
target = event.target if event.type == 'pubmsg' else event.source.nick
sender = event.source.nick
args = event.arguments[0].split()[1:]
if len(args) == 0:
return bot.connection.privmsg(target, 'This command only works if you tell me what channel to leave.')
if args[0][0] != '#':
return bot.connection.privmsg(target, f'"{args[0]}" is not a channel!')
if args[0] == '##voidwalker':
return bot.connection.privmsg(target, "I am forbidden from leaving my master's realm.")
bot.connection.part(args[0], message=f':{sender} has sent me to a far off land!')
logs.info(f'{sender} has removed us from {args[0]}')
help_str = 'Part the supplied channel. (Requires Trusted)'
CommandHandler.commands.append(Command('partf', partf, restriction=Command.TRUSTED, help=help_str))
def help(bot, event):
"""Find help strings."""
target = event.target if event.type == 'pubmsg' else event.source.nick
args = event.arguments[0].split()[1:]
if len(args) == 0:
return bot.connection.privmsg(target, 'Please see https://meta.miraheze.org/wiki/User:Void-bot/Help')
command = CommandHandler.get_command(args[0])
if command is False or command.help is False:
return bot.connection.privmsg(target, f'Sorry, I could not find help on {args[0]}.')
bot.connection.privmsg(target, command.help)
help_str = 'Provides general help, or help on a supplied command.'
CommandHandler.commands.append(Command('help', help, restriction=Command.GENERAL, help=help_str))
def access(bot, event):
"""Report user's access levels."""
target = event.target if event.type == 'pubmsg' else event.source.nick
sender = event.source.nick
dummy = CommandHandler(event, bot)
access_level = dummy.perm_level()
bot.connection.privmsg(target, f'{sender} has level {access_level} clearance.')
help_str = 'Tells you what kind of access you have.'
CommandHandler.commands.append(Command('access', access, restriction=Command.GENERAL, help=help_str))
def log(bot, event):
"""Perform logging."""
if event.target == '#miraheze-cvt':
api = bot.apis['cvt']
page = 'CVT action log'
header = '== Log =='
source = event.source.nick
format = '* <%s> %L --~~~~~'
try:
log_entry = Logger.irc_entry(event, format)
Logger(api, page, header, source, log_entry).run()
bot.connection.privmsg(event.target, f'Saved item "{event.arguments[0][5:]}"')
logs.info(f'{source} issued cvt log; {event.arguments[0][5:]}')
except (ConnectionError, ApiError) as e:
bot.connection.privmsg(event.target, 'Failed to save item!')
logs.exception(e)
elif event.target == '#testadminwiki':
api = bot.apis['testadminwiki']
page = 'Test Wiki: Server admin log'
now = datetime.utcnow()
header = now.strftime('== %Y-%m-%d ==')
source = event.source.nick
format = '* %H:%M %s: %L'
try:
log_entry = Logger.irc_entry(event, format)
Logger(api, page, header, source, log_entry).run()
bot.connection.privmsg(event.target, f'Saved item "{event.arguments[0][5:]}"')
logs.info(f'{source} issued sal log; {event.arguments[0][5:]}')
except (ConnectionError, ApiError) as e:
bot.connection.privmsg(event.target, 'Failed to save item!')
logs.exception(e)
elif event.target == '#fossbots':
api = bot.apis['botwiki']
page = 'FOSSBotsWiki:Server admin log'
now = datetime.utcnow()
header = now.strftime('== %Y-%m-%d ==')
source = event.source.nick
format = '* <%s> %L --~~~~~'
try:
log_entry = Logger.irc_entry(event, format)
Logger(api, page, header, source, log_entry).run()
bot.connection.privmsg(event.target, f'Saved item "{event.arguments[0][5:]}"')
logs.info(f'{source} issued botwiki SAL; {event.arguments[0][5:]}')
except (ConnectionError, ApiError) as e:
bot.connection.privmsg(event.target, 'Failed to save item!')
logs.exception(e)
help_str = 'Does cvt log and testadminwiki server admin log.'
CommandHandler.commands.append(Command('log', log, restriction=Command.VOICED, help=help_str))
def ping(bot, event):
"""Check connection."""
if event.target == '##voidwalker':
bot.check_connection()
help_str = "Debug only. Please don't play with this!"
CommandHandler.commands.append(Command('ping', ping, restriction=Command.DEVELOPER, help=help_str))