-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
564 lines (454 loc) · 19 KB
/
Copy pathbot.py
File metadata and controls
564 lines (454 loc) · 19 KB
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
# Work with Python 3.6
from posixpath import abspath
import discord
from discord.ext import commands, tasks
import json, operator
import os
import requests
from datetime import datetime
import logging, sys
import re
from urllib.parse import urlparse
import urllib
import asyncio
import time
import pickle
# custom functions
from commands.questionOfTheDay import questionOfTheDay
#from commands.politicalTest import politicalTest
from commands.points import bonk
from commands.points import based
from commands.points import cringe
from commands.points import dum
from commands.points import smart
from commands.SPCEvents import scan
# read secrets
with open('secrets.txt') as f:
TOKEN = str(f.readline())
client = discord.Client()
client = commands.Bot(command_prefix = '!!')
cacheFileName = 'cache'
ignoreChannels = [729885603591618601, # roles channel
409910783158255616, # announcement channel
624759106930081812, # notification channel
450381990470877186, # bot spam channel
747930008835588156, # helping bot 3
803789404723871805, # qotd question channel
882728909991460884, # announcments
883093396892295238, # server events
882732242202411059, #roles
882759177620033566 # qotd
]
pictureFileFormats = ['jpg', 'png', 'gif', 'jpeg', 'tiff', 'bmp']
# loading server settings
with open('./commands/serverInfo/serverDatabase.json', encoding='utf-8') as file:
serverSettings = json.load(file)
DEBUG = False
def debugLog(s):
if DEBUG:
print(s)
def getInfoMessage(reaction):
return reaction.message
def getInfoEmoji(reaction):
return reaction.emoji
def inCache(id):
with open(cacheFileName, 'r') as cache:
for line in cache:
if str(id) in line:
return int(next(cache))
cache.close()
return False
def writeToCache(message, starBoard):
with open(cacheFileName, 'a') as cache:
cache.write(str(message.id) + '\n')
cache.write(str(starBoard.last_message_id) + '\n')
cache.close()
def isEmojiAvailable(id):
for emoji in client.emojis:
if id == emoji.id:
return True
return False
def urlValid(url):
req = Request(url)
try:
response = urlopen(req)
except(URLError, e):
if hasattr(e, 'reason'):
return False
elif hasattr(e, 'code'):
return False
else:
return True
@client.event
async def on_ready():
await client.change_presence(status=discord.Status.online, activity=discord.Game("!!h for help"))
print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + ' \nRinBot is ONLINE, listening for actions...\n')
@tasks.loop(seconds=30)
async def checkEvents():
'''
reading spc events
'''
'''
scanning for new ones
'''
scanTime = '15:00'
timeformat = '%H:%M'
timeNow = datetime.strftime(datetime.now(), timeformat)
rightNow = datetime.now()
if rightNow.strftime("%M") == "22":
print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + ' -> Scanning for SPC Events...')
await scan.main(client=client)
@tasks.loop(seconds=30)
async def checkTime():
'''
Im too tired to implement this,
but because of the way I'm doing looped tasks I cannot use ctx
so this task should loop every 30 seconds
it should read the server settings, interating over every server entry
it should then read the time they want their questions
if that time is right now, it should run the qotd functions
then interating for the next server
'''
timeformat = '%H:%M'
timeNow = datetime.strftime(datetime.now(), timeformat)
# read server settings to see if they want questions
for server in serverSettings: # server in specific interation is serverSettings[server]
print('Server: ' + str(server))
#print('Server Settings: ' + str(serverSettings[server]))
if serverSettings[server]['questionOfTheDay']['enabled']:
# QOTD IS ENABLED
print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + ' QOTD ' + str(server) + ': ENABLED')
timeQOTD = serverSettings[server]['questionOfTheDay']['time']
channelQOTD = client.get_channel(serverSettings[server]['questionOfTheDay']['channelID'])
print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + ' Time now: ' + str(timeNow))
print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + ' QOTD time: ' + str(timeQOTD))
print('-------------------------------------------------------------------------------------------')
if (str(timeNow) == str(timeQOTD)):
print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + ' Attempting to post QOTD to ' + str(server))
await questionOfTheDay.getQuestion(ctx=None, client=client, randomQuestion=None, serverSettings=serverSettings, server=server)
await asyncio.sleep(60)
else:
# QOTD NOT ENABLED
print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + ' QOTD ' + str(server) + ': DISABLED')
print('-------------------------------------------------------------------------------------------')
'''
# load the pickle
#try:
print('trying to load the cal pickle')
pickle_in = open('./commands/SPCEvents/dict.pickle', 'rb')
cal = pickle.load(pickle_in)
total_events = cal
print('pickle loaded')
#print(cal)
interation = 0
for event in cal:
'''
#get relative days
#then if it is 3 or less days
#post it
#move to next object
#exit loopcd
#remove from pickle list
#save pickle list
'''
#print('event: ' + str(event))
#print('event datetime: ' + str(event['dateTime']))
#diff = datetime.now() - event['dateTime']
diff = event['dateTime'] - datetime.now()
#print('day diff = ' + str(diff.days))
total_events = cal
notice_in_days = 2
if ((int(diff.days) >= 0) and (int(diff.days) <= notice_in_days)): # event in time windows
print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + ' -> ' + event['title'] + ' is happening in ' + str(diff.days) + ' days')
await scan.postEvent(eventInfo=event, client=client)
cal.pop(interation)
await asyncio.sleep(30)
else:
if (int(diff.days) < 0): # event passed
#print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + ' -> ' + event['title'] + ' has already passed (' + str(diff.days) + ' days)... Removing from cal')
cal.pop(interation)
elif (int(diff.days) > notice_in_days): # in the future
pass
#print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + ' -> ' + event['title'] + ' event too far into the future... (' + str(diff.days) + ' days)')
#print('diffDays = ' + str(diff.days))
interation += 1
cal = total_events
pickle_out = open('./commands/SPCEvents/dict.pickle', 'wb')
pickle.dump(cal, pickle_out)
pickle_out.close()'''
#except Exception as e:
# print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + ' Calender pickle could not be read.')
# print(e)
# pass
@checkTime.before_loop
async def before():
await client.wait_until_ready()
print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + ' Time has finished waiting')
@client.event
async def on_reaction_add(reaction, user):
# get info
message = getInfoMessage(reaction)
emoji = getInfoEmoji(reaction)
serverID = message.guild.id
# variables that wont work globally cause im dumb and lazy
with open('./commands/serverInfo/serverDatabase.json', encoding='utf-8') as file:
serverSettings = json.load(file)
starBoard = client.get_channel(serverSettings[str(serverID)]['starBoard']['channelID'])
minReact = serverSettings[str(serverID)]['starBoard']['minimumReact']
#leaderBoard = client.get_channel(starBoardLeaderBoardID)
containsVideo = False
# does the reaction count of this emoji outnumber the min reaction set?
# does this reaction take place in an allowed channel?
# is the emoji even available?
# and in cache ( this is a new message )
if ((reaction.count >= minReact) and
(message.channel.id not in ignoreChannels) and
((reaction.custom_emoji == False) or (isEmojiAvailable(emoji.id))) and
(inCache(message.id) != False)):
debugLog('Found in cache...')
debugLog((str(emoji.name) if reaction.custom_emoji else str(emoji)) + ' added to message ' + str(message.id) + ', total ' + (str(emoji.name) if reaction.custom_emoji else str(emoji)) + ': ' + str(reaction.count))
messageToUpdate = await starBoard.fetch_message(inCache(message.id))
content = []
for item in message.reactions:
if (item.count >= minReact) and ((item.custom_emoji == False) or isEmojiAvailable(item.emoji.id)):
content.append(str(item.emoji) + ' **' + str(item.count) + '** ')
convertList = [str(element) for element in content] # convert the list
await messageToUpdate.edit(content=(' , '.join(convertList))) # edit the bot message to the correct list and number of emojis
elif ((reaction.count >= minReact) and
(message.channel.id not in ignoreChannels) and
((reaction.custom_emoji == False) or isEmojiAvailable(emoji.id)) and
(inCache(message.id) == False)): # if this is a new star message
debugLog((str(emoji.name) if reaction.custom_emoji else str(emoji)) + ' added to message ' + str(message.id) + ', total ' + (str(emoji.name) if reaction.custom_emoji else str(emoji)) + ': ' + str(reaction.count))
starEmbed = discord.Embed(
title = str(message.author),
description = str(message.content),
)
starEmbed.set_footer(text='ID: ' + str(message.id) + ' - ' + str(message.created_at.strftime('%m/%d/%Y, %I:%M:%S %p EST')))
starEmbed.set_author(name=str(message.author), icon_url=str(message.author.avatar_url))
starEmbed.add_field(name='Source', value="[Jump!](" + message.jump_url + ")", inline=False)
# checking media
try: # trying to look for linked media
if urlValid(re.search("(?P<url>https?://[^\s]+)", message.content).group("url")): # is a url
if any(x in re.search("(?P<url>https?://[^\s]+)", message.content).group("url") for x in pictureFileFormats): # if it has a picture linked
starEmbed.set_image(url=str(re.search("(?P<url>https?://[^\s]+)", message.content).group("url")))
debugLog('Attached linked image... ' + re.search("(?P<url>https?://[^\s]+)", message.content).group("url"))
else: # something is linked but its not a picture, this will make it so that will upload the file
r = requests.get(message.attachments[0].url, allow_redirects=True)
open('temp', 'wb').write(r.content)
containsVideo = True
else: # if there was an attached image
if len(message.attachments) > 0:
if any(x in message.attachments[0].url for x in pictureFileFormats): # picture was attached
starEmbed.set_image(url=str(message.attachments[0].url))
debugLog('Attached sent image...')
else: # something else wat attached
r = requests.get(message.attachments[0].url, allow_redirects=True)
open('temp', 'wb').write(r.content)
containsVideo = True
except: # if there was nothing linked and it made and exception
if len(message.attachments) > 0:
if any(x in message.attachments[0].url for x in pictureFileFormats):
starEmbed.set_image(url=str(message.attachments[0].url))
debugLog('Attached sent image...')
else:
r = requests.get(message.attachments[0].url, allow_redirects=True)
open('temp', 'wb').write(r.content)
containsVideo = True
if containsVideo:
await starBoard.send(str(reaction.emoji) + ' **' + str(reaction.count) + '** ',embed=starEmbed, file=(discord.File('temp')))
else:
await starBoard.send(str(reaction.emoji) + ' **' + str(reaction.count) + '** ',embed=starEmbed)
# writing the message to cache
writeToCache(message, client.get_channel(serverSettings[str(serverID)]['starBoard']['channelID']))
@client.command()
async def starMin(ctx):
with open('./commands/serverInfo/serverDatabase.json', encoding='utf-8') as file:
serverSettings = json.load(file)
await ctx.send("Minimum reactions to get on the star board is " + str(serverSettings[str(ctx.guild.id)]['starBoard']['minimumReact']) + " currently.")
# Bonk Points
@client.command(name="bonk")
async def _bonk(ctx, user=None, client=client):
# read user
with open('./commands/usersInfo/userDatabase.json', encoding='utf-8') as file:
users = json.load(file)
# copying the mention system from userInfo
if user == None:
await ctx.send('Hey dummy! you didn\'t tell me who the horny is!')
return
try:
user = ctx.message.mentions[0]
except:
print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + " Failed obtaining user")
return
await bonk.addBonk(ctx=ctx, client=client, user=user, userDatabase=os.path.abspath('./commands/usersInfo/userDatabase.json'))
@client.command(name="bonks")
async def _bonks(ctx, user=None, client=client):
if user == None:
await ctx.send('Hey dummy! you didn\'t tell me who the horny is!')
return
try:
user = ctx.message.mentions[0]
except:
print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + " Failed obtaining user")
return
await bonk.getBonks(ctx=ctx, client=client, user=user, userDatabase=os.path.abspath('./commands/usersInfo/userDatabase.json'))
# Based Points
@client.command(name="based")
async def _based(ctx, user=None, client=client):
# read user
with open('./commands/usersInfo/userDatabase.json', encoding='utf-8') as file:
users = json.load(file)
# copying the mention system from userInfo
if user == None:
await ctx.send('Hey dummy! you didn\'t tell me who the based is!')
return
try:
user = ctx.message.mentions[0]
except:
print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + " Failed obtaining user")
return
await based.addBased(ctx=ctx, client=client, user=user, userDatabase=os.path.abspath('./commands/usersInfo/userDatabase.json'))
@client.command(aliases=['isBased', 'getBased'])
async def _isBased(ctx, user=None, client=client):
if user == None:
await ctx.send('Hey dummy! you didn\'t tell me who\' based!')
return
try:
user = ctx.message.mentions[0]
except:
print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + " Failed obtaining user")
return
await based.getBased(ctx=ctx, client=client, user=user, userDatabase=os.path.abspath('./commands/usersInfo/userDatabase.json'))
# Cringe Points
@client.command(name="cringe")
async def _cringe(ctx, user=None, client=client):
# read user
with open('./commands/usersInfo/userDatabase.json', encoding='utf-8') as file:
users = json.load(file)
# copying the mention system from userInfo
if user == None:
await ctx.send('Hey dummy! you didn\'t tell me who\'s cringe is!')
return
try:
user = ctx.message.mentions[0]
except:
print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + " Failed obtaining user")
return
await cringe.addCringe(ctx=ctx, client=client, user=user, userDatabase=os.path.abspath('./commands/usersInfo/userDatabase.json'))
@client.command(aliases=['isCringe', 'getCringe'])
async def _isCringe(ctx, user=None, client=client):
if user == None:
await ctx.send('Hey dummy! you didn\'t tell me who\'s cringe!')
return
try:
user = ctx.message.mentions[0]
except:
print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + " Failed obtaining user")
return
await cringe.getCringe(ctx=ctx, client=client, user=user, userDatabase=os.path.abspath('./commands/usersInfo/userDatabase.json'))
# Dum Points
@client.command(aliases=['dum', 'dumb'])
async def _Dum(ctx, user=None, client=client):
# read user
with open('./commands/usersInfo/userDatabase.json', encoding='utf-8') as file:
users = json.load(file)
# copying the mention system from userInfo
if user == None:
await ctx.send('Hey dummy! you didn\'t tell me who\'s dum!')
return
try:
user = ctx.message.mentions[0]
except:
print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + " Failed obtaining user")
return
await dum.addDum(ctx=ctx, client=client, user=user, userDatabase=os.path.abspath('./commands/usersInfo/userDatabase.json'))
@client.command(aliases=['isDum', 'getDum', 'isDumb', 'getDumb'])
async def _isDum(ctx, user=None, client=client):
if user == None:
await ctx.send('Hey dummy! you didn\'t tell me who\'s cringe!')
return
try:
user = ctx.message.mentions[0]
except:
print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + " Failed obtaining user")
return
await dum.getDum(ctx=ctx, client=client, user=user, userDatabase=os.path.abspath('./commands/usersInfo/userDatabase.json'))
# Smart Points
@client.command(aliases=['smart', 'smort'])
async def _smart(ctx, user=None, client=client):
# read user
with open('./commands/usersInfo/userDatabase.json', encoding='utf-8') as file:
users = json.load(file)
# copying the mention system from userInfo
if user == None:
await ctx.send('Hey dummy! you didn\'t tell me who\'s smart!')
return
try:
user = ctx.message.mentions[0]
except:
print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + " Failed obtaining user")
return
await smart.addSmart(ctx=ctx, client=client, user=user, userDatabase=os.path.abspath('./commands/usersInfo/userDatabase.json'))
@client.command(aliases=['isSmart', 'getSmart', 'isSmort', 'getSmort'])
async def _isSmart(ctx, user=None, client=client):
if user == None:
await ctx.send('Hey dummy! you didn\'t tell me who\'s smart!')
return
try:
user = ctx.message.mentions[0]
except:
print(datetime.now().strftime("%Y-%m-%d %I:%M:%S:%f %p") + " Failed obtaining user")
return
await smart.getSmart(ctx=ctx, client=client, user=user, userDatabase=os.path.abspath('./commands/usersInfo/userDatabase.json'))
@client.command(name="forceQuestion")
async def forceQuestion(ctx, question=None, client=client, qotdChannel=882759177620033566):
print(serverSettings[str(ctx.guild.id)])
if ctx.message.author.id == 183680648408465408:
if question != None:
print("question is not none")
print(question)
question = int(question)
await questionOfTheDay.getQuestion(
ctx=ctx,
client=client,
randomQuestion=question,
questionBoard=qotdChannel,
server=str(ctx.guild.id),
serverSettings=serverSettings
)
#await questionOfTheDay.getQuestion(ctx=ctx, client=client, randomQuestion=question, questionBoard=qotdChannel, server=[str(ctx.guild.id)], serverSettings=serverSettings[str(ctx.guild.id)])
@client.command()
@commands.has_any_role(893262934191530046, 882724660331049000, 882724801712631918, 882724899821600778, 882730350470651904, 883076311743103037)
async def playlistAppend(ctx, *, message):
try:
playlist = message.split()[0]
link = message.split()[1]
# maybe in the future when I am running the
# bot all off the storage I can use this
# until then I'm going to use this
playlistPath = '/mnt/nas/personal/github/MusicBot/Playlists/' + playlist + '.txt'
'''
playlistPath = str(os.getcwd())
playlistPath = playlistPath.replace('spartanBot', '') + 'MusicBot/Playlists/' + playlist + '.txt'
'''
with open(playlistPath, 'a') as file:
file.write('\n' + link)
await ctx.send('Song was added to ' + playlist)
except Exception as e:
print(e)
await ctx.send('There was an error adding your song. Are you sure that playlist exists or that you spelled it correctly?')
@client.command()
async def h(ctx):
embed = discord.Embed(
title = "Help",
description = "RinBot is a star board bot made for the server.\nPosts that are good tend to get reactions from other users.",
color = 9834751,
)
embed.add_field(name='starMin', value='Tells you the minimum reactions needed to get on the star board', inline=False)
await ctx.send(embed=embed)
'''
STARTING TIME WATCH
'''
checkTime.start()
checkEvents.start()
client.run(TOKEN)