-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
executable file
·596 lines (462 loc) · 18.6 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#!/usr/bin/env python3.8
import asyncio, random, os, sys, re, json, time
import discord
from datetime import datetime
from discord.ext import commands, tasks
#google image search dependancy
from google_images_download import google_images_download
#wordcloud dependencies
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
# Note, requires envvar BOT_TOKEN
bot = commands.Bot(command_prefix="::")
# This stores the current nicknames used by force_nick
nickname_dir = {}
muted_l = []
# These variables store voting information
vote = False # votemute active or not
votes = {'yes':0, 'no':0} # vote tally
voters = [] # list of voters
muted = {} # list of muted people
u_vote = False # voteunmute active or not
u_votes = {'yes':0, 'no':0} # vote tally
u_voters = [] # list of voters
def is_admin(user):
"""
Checks whether user is an administrator on the guild
"""
return user.guild_permissions.administrator
@bot.event
async def on_ready(): # runs upon bot being initialized
print("ready!")
auto_unmute.start() # start auto_unmute loop
muted_list_export.start() # start muted_list_export loop
@bot.event
async def on_member_join(member): # runs when a new member joins the server
if member in muted: # checks if member is in the muted list
role = discord.utils.get(member.guild.roles, name='muted by the people') # gets the role name
await member.add_roles(role) # assigns the role
else:
pass # TO-DO: add a default 'member' role to all users upon joining
@bot.event
async def on_message(message):
"""
Fill up 'log.txt' with messages as they are sent in channels with bot
TODO: - Add separate log.txts for separate channels:w
- Clear log files as they get too large
"""
global muted_l
global votes
global voters
global u_votes
global u_voters
message_content = message.content # store message content in a variable
author = message.author # grab the author name from the message
if author == bot.user: # make sure the message was not sent by the bot
return
if vote: # check if votemute is active
if (message_content == 'yes') and not(author in voters): # check if someone typed 'yes'
votes['yes'] = votes['yes'] + 1 # add 1 to votes counter
voters.append(author) # add author to voters list
print(f'{author} voted yes')
elif (message_content == 'no') and not(author in voters): # check if someone typed 'no'
votes['no'] = votes['no'] + 1 # add 1 to the no counter
voters.append(author) # add author to voters list
print(f'{author} voted no')
elif u_vote: # check if voteunmute is active
if (message_content == 'yes') and not(author in u_voters):
u_votes['yes'] = u_votes['yes'] + 1
u_voters.append(author)
print(f'{author} voted yes')
elif (message_content == 'no') and not(author in u_voters):
u_votes['no'] = u_votes['no'] + 1
u_voters.append(author)
print(f'{author} voted no')
else:
pass # TO-DO:To be decided
message_content = message.content
for i in muted_l:
if re.match(i[0], message_content):
await message.delete()
with open("log.txt", "a") as f:
time_stamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
f.write(f"<{time_stamp}>{message_content}\n")
await bot.process_commands(message)
@tasks.loop(seconds=10) # TO-DO
async def muted_list_export(): # exports the list of muted users every 10 seconds
muted_json = json.dumps(muted) # this function needs work, is not finished yet.
dir = 'muted.json'
with open(dir, 'w+') as f:
json.dump(muted_json, f)
@tasks.loop(seconds=10) # TO-DO
async def muted_list_import(): # imports the list of muted users every 10 seconds
#global
#dir = 'muted.json'
#with open(dir, 'w+') as f:
#muted = json.load(f)
pass
@tasks.loop(seconds=10) # TO-DO
async def auto_unmute(): # auto unmutes people and updates the muted
for member in muted:
if time.time() - muted[member] >= 900:
role = discord.utils.get(member.guild.roles, name='muted by the people')
await member.remove_roles(role)
print(f'auto_unmuted: unmuted {member}')
del muted[member]
@commands.command()
async def votemute(ctx, member:discord.Member=None):
print('{} - Command: votemute | Author: {}'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),ctx.author))
global vote
global votes
global voters
global muted
guild = ctx.guild # get guild(server) information
if vote or u_vote: # make sure only 1 voting session is active at a time
await ctx.send('a vote is already in progress, please wait')
return
vote = True
await ctx.send(f'vote to mute started by {ctx.message.author}')
await ctx.send('reply with [yes] or [no] to vote')
await ctx.send('waiting 60 seconds for votes')
await asyncio.sleep(30)
await ctx.send('waiting 30 seconds for votes')
await asyncio.sleep(20)
await ctx.send('waiting 10 seconds for votes')
await asyncio.sleep(10)
yes_votes = votes['yes'] # get amount of yes votes
no_votes = votes['no'] # get amount of no votes
if yes_votes > no_votes: # compare results
await ctx.send('the majority has voted yes, user will be muted for 15 minutes')
role = discord.utils.get(member.guild.roles, name='muted by the people') # get the muted role
await member.add_roles(role) # assign the muted role
muted[member] = time.time() # add member to the muted list with a time stamp
print(f'{member} has been muted for 15 minutes')
for invite in await guild.invites(): # remove muted users invites
if invite.inviter == member:
await invite.delete()
else:
pass
if no_votes >= yes_votes:
await ctx.send('the majority voted no, user will not be muted')
vote = False
votes = {'yes':0, 'no':0}
print('{} - Task Finished Succesfully'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
@commands.command()
async def voteunmute(ctx, member:discord.Member=None):
print('{} - Command: voteunmute | Author: {}'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),ctx.author))
global u_vote
global u_votes
global u_voters
if vote or u_vote:
await ctx.send('a vote is already in progress, please wait')
return
u_vote = True
await ctx.send(f'vote to unmute started by {ctx.message.author}')
await ctx.send('reply with [yes] or [no] to vote')
await ctx.send('waiting 60 seconds for votes')
await asyncio.sleep(30)
await ctx.send('waiting 30 seconds for votes')
await asyncio.sleep(20)
await ctx.send('waiting 10 seconds for votes')
await asyncio.sleep(10)
yes_votes = u_votes['yes']
no_votes = u_votes['no']
if yes_votes > no_votes:
await ctx.send('the majority has voted yes, user will be unmuted')
role = discord.utils.get(member.guild.roles, name='muted by the people')
await member.remove_roles(role)
print(f'{member} has been unmuted')
if no_votes >= yes_votes:
await ctx.send('the majority voted no, user will not be unmuted')
u_vote = False
u_votes = {'yes':0, 'no':0}
print('{} - Task Finished Succesfully'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
@commands.command()
async def unmute_all(ctx):
"""
removes all values from mute list
"""
print('{} - Command: unmute_all | Author: {}'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),ctx.author))
if not is_admin(ctx.author):
await ctx.send("You're not allowed to use this command!")
print("Not allowed.")
return
global muted_l
muted_l = []
print('{} - Task Finished Succesfully'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
@commands.command()
async def mute_where(ctx, text):
"""
adds text to "muted_l"
text in muted_l will be deleted when sent.
"""
print('{} - Command: mute_where | Author: {}'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),ctx.author))
global muted_l
if not is_admin(ctx.author):
await ctx.send("You're not allowed to use this command!")
print("Not allowed.")
return
try:
muted_re = re.compile(text, re.I)
muted_l.append((muted_re, text))
except:
await ctx.send(f"```{text}``` is not valid regex.")
print("Invalid regex")
return
print('{} - Task Finished Succesfully'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
@commands.command()
async def unmute_where(ctx, text):
"""
removes text to "muted_l"
text in muted_l will be deleted when sent.
"""
print('{} - Command: unmute_where | Author: {}'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),ctx.author))
global muted_l
if not is_admin(ctx.author):
await ctx.send("You're not allowed to use this command!")
print("Not allowed.")
return
for i, t in enumerate(muted_l):
if t[1] == text:
muted_l = muted_l[:i] + muted_l[i+1:]
print('{} - Task Finished Succesfully'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
@commands.command()
async def mute_status(ctx):
"""
sends status of muted text
NOTE: THIS IS BUSTED ATM
"""
print('{} - Command: mute_status | Author: {}'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),ctx.author))
global muted_l
if not is_admin(ctx.author):
await ctx.send("You're not allowed to use this command!")
print("Not allowed.")
return
await ctx.send(repr(muted_l))
print('{} - Task Finished Succesfully'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
@commands.command()
async def google(ctx, *, keywords:str):
"""
Finds a random google image with a given keyword
"""
print('{} - Command: google | Author: {}'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),ctx.author))
response = google_images_download.googleimagesdownload() #class instantiation
arguments = {"keywords":keywords,"limit":5,"print_urls":False, "format":"jpg", "safe_search":True} #creating list of arguments
paths = response.download(arguments) #passing the arguments to the function
my_files = [
discord.File(paths[0][keywords][random.randint(0,4)]),
]
await ctx.send(files=my_files)
print('{} - Task Finished Succesfully'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
@commands.command()
async def force_nick(ctx, *args):
"""
Force nickname on user -- set nickname, and then periodically monitors
user for changes, and set back when it happens
"""
print('{} - Command: force_nick | Author: {}'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),ctx.author))
if not is_admin(ctx.author):
await ctx.send("You're not allowed to use this command!")
print("Not allowed.")
return
if len(args) != 2:
await ctx.send("Invalid number of args.\n\n"\
"Usage:\n\t`::force_nick user nick`")
return
if len(ctx.message.mentions) != 1:
await ctx.send("Invalid mention.\n"\
"Usage:\n\t`::force_nick user nick`")
return
user = ctx.message.mentions[0]
nickname_dir[user.id] = args[1]
# Make a closure which will check the user's nick for us
def mk_nick_check(user, nickname):
async def nick_check():
while user.id in nickname_dir:
if user.nick != nickname:
await user.edit(nick=nickname)
await asyncio.sleep(10)
return nick_check
chk = mk_nick_check(ctx.message.mentions[0], args[1])
loop = asyncio.get_event_loop()
loop.create_task(chk())
print('{} - Task Finished Succesfully'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
@commands.command()
async def reset_nick(ctx, *args):
"""
Reset forced nickname on user
"""
print('{} - Command: reset_nick | Author: {}'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),ctx.author))
if not is_admin(ctx.author):
await ctx.send("You're not allowed to use this command!")
print("Not allowed.")
return
if len(args) != 1:
await ctx.send("Invalid # of arguments.\n"\
"Usage:\n\t`::reset_nick user`")
return
if len(ctx.message.mentions) != 1:
await ctx.send("invalid mention.\n"\
"Usage:\n\t`::reset_nick user`")
return
del nickname_dir[ctx.message.mentions[0].id]
await ctx.message.mentions[0].edit(nick=None)
print('{} - Task Finished Succesfully'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
@commands.command()
async def wordcloud(ctx):
"""
create wordcloud from all words in log.txt
TODO: - Individual wordclouds per channel
"""
print('{} - Command: wordcloud | Author: {}'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),ctx.author))
cache_size = len(bot.cached_messages)
print(f'cache size: {cache_size}')
messages = []
for message in bot.cached_messages: # returns a discord.Message object
messages.append(message.content)
text = ' '.join(messages)
wordcloud = WordCloud(max_font_size=50, max_words=1000, background_color="white").generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
wordcloud.to_file('word_cloud.png')
file = discord.File('word_cloud.png')
await ctx.channel.send(file=file)
if os.path.exists('word_cloud.png'):
os.remove('word_cloud.png')
print('{} - Task Finished Succesfully'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
@commands.command()
async def emojify(ctx, *, content:str):
"""
Emojify the input
"""
print('{} - Command: emoji | Author: {}'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),ctx.author))
normalText=list(content.lower())
emojiText = []
for i in normalText:
if i == '0':
emojiText.append(':zero:')
elif i == '1':
emojiText.append(':one:')
elif i == '2':
emojiText.append(':two:')
elif i == '3':
emojiText.append(':three:')
elif i == '4':
emojiText.append(':four:')
elif i == '5':
emojiText.append(':five:')
elif i == '6':
emojiText.append(':six:')
elif i == '7':
emojiText.append(':seven:')
elif i == '8':
emojiText.append(':eight:')
elif i == '9':
emojiText.append(':nine:')
elif i == 'b':
emojiText.append(':b:')
elif i == ' ':
emojiText.append(' ')
elif re.search("[a-z]", i):
emojiText.append(':regional_indicator_{}:'.format(i))
else:
emojiText.append(i)
fullStr = ' '.join(emojiText)
await ctx.send(fullStr)
print('{} - Task Finished Succesfully'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
@commands.command()
async def ping(ctx):
await ctx.send('pang')
@commands.command()
async def echo(ctx, n:int, *, content:str):
"""
Echoes a message, !echo [amount of times to echo] [message here]
"""
print('{} - Command: echo | Author: {}'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),ctx.author))
index = 0
limit = 15
if n > 0:
while index < min(n,limit):
await ctx.send(content)
index += 1
else:
await ctx.send(content)
print('{} - Task Finished Succesfully'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
@commands.command()
async def coinflip(ctx):
"""
Flip a coin
"""
options=['Heads','Tails']
await ctx.send('You rolled ' + options[random.randint(0,1)])
@commands.command()
async def yank(ctx, *args):
"""
Yank messsages from a certain channel and output them to a text file
File will be in folder 'yanks'
"""
print('{} - Command: yank | Author: {}'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),ctx.author))
if not is_admin(ctx.author):
await ctx.send("You're not allowed to use this command!")
print("Not allowed")
return
if len(args) != 2:
await ctx.send(\
'Invalid number of arguments -- must specify mode and limit\n'\
'Mode options are:\n\t`text->only capture msg text`\n'\
'\t`full->capture author, timestamp, text`\n\n'
'Limit options are: number or "none"\n'\
'Note: none limit is stressing on the API. Do this sparingly.\n\n'\
'example: `::yank text 123`\n\n'\
'This will capture the text of the latest 123 messages in this channel.'
)
return
if args[0] not in ("text", "full"):
await ctx.send("Mode must be either `text`, or `full`")
return
else:
mode = args[0].strip()
if args[1].strip() == "none":
msg_limit = None
oldest_first = True
elif args[1].strip().isdigit():
msg_limit = int(args[1])
oldest_first = False
else:
await ctx.send("Invalid limit")
return
filepath = f"yanks/{datetime.now().strftime('%H-%M-%S')}-{args[0]}-{ctx.channel}"
await ctx.send(f'Starting yanking channel {ctx.channel}\n' \
f'Outputting to `{filepath}`')
if not os.path.isdir("./yanks"):
os.mkdir("./yanks")
with open(filepath, "w+") as wf:
async for msg in ctx.history(limit=msg_limit, oldest_first=oldest_first):
if mode == 'full':
wf.write(f'{msg.author} - {msg.created_at.strftime("%d/%m/Y|%H:%M:%S")} - {repr(msg.content)}\n\n')
elif mode == 'text':
wf.write(f'{repr(msg.content)}\n\n')
print('{} - Task Finished Succesfully'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
def main():
if 'BOT_TOKEN' not in os.environ:
print("Missing BOT_TOKEN environment variable.")
sys.exit(1)
bot.add_command(ping)
# bot.add_command(echo)
bot.add_command(mute_where)
bot.add_command(unmute_where)
bot.add_command(unmute_all)
bot.add_command(mute_status)
bot.add_command(google)
bot.add_command(coinflip)
bot.add_command(emojify)
bot.add_command(yank)
bot.add_command(wordcloud)
bot.add_command(force_nick)
bot.add_command(reset_nick)
bot.add_command(votemute)
bot.add_command(voteunmute)
bot.run(os.environ['BOT_TOKEN'])
if __name__ == "__main__":
main()