This repository has been archived by the owner on Oct 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DLBot.py
516 lines (450 loc) · 17.6 KB
/
DLBot.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
## Notes
# Url to connect tht bot to a server: https://discordapp.com/oauth2/authorize?&client_id=170493165214629888&scope=bot
# id 129340698871726080
# Basic informations. To change if you want to setup your own Bot.
__program__ = "DLBot"
__version__ = "0.2.0b"
## Libaries import
# Testing Initial import
import sys
try:
assert sys.version_info >= (3, 4)
from discord.ext import commands
import discord
except ImportError:
print("Discord.py is not installed.\n"
"Consult the guide for your operating system "
"and do ALL the steps in order.\n")
sys.exit()
except AssertionError:
print("DLBot needs Python 3.4 or superior.\n"
"Consult the guide for your operating system "
"and do ALL the steps in order.\n")
sys.exit()
# Discord imports
import asyncio
import aiohttp
import websockets
# other imports
import requests
import os
import pickle
import datetime
import random
import feedparser
import re
import setup
##Variables setup
#BASE STRING : change it if you want, I don't care
base = ".."
#Message sent when the user call for help
helptext = """
Here are my commands :
-help : Show this text. Use help aCommand to get the command definition
-hello : Hi !
-xdy : Roll some dices ! (ex : 2d100) It will give you x answers between 0 and y
-galnet : Will give you random GalNet feed unless you specify an index (galnet [i])
-wiki : Will give you a wikipedia definition
-pict : NOT YET IMPLEMENTED
-yt : Put some videos in your channel !
-quote : When funny things are said, you must save them !
Some commands are not detailed. Good luck finding them !
"""
#List of my functions
functions=['help', 'hello', 'roll', 'jdrstart', 'jdrstop', 'galnet', 'wiki', 'pict', 'yt', 'quote']
#WTF IS THAT ? That's the documentation dictionary !
helpdict = dict(help=base+"""help [command|all] :
Why are you wanting help on help anyway ?
command : function handled by the bot
all : if you wan all the documentation"""
, hello=base+"""hello
Hello @DLBot :
It will say Hello. That's all !"""
, roll=base+"""roll {x}d{y} :
Used in roleplay games. Like rolling 1 dice containing 20 faces will be 1d20
x : number between 1 and 10
y : number greater than 1"""
, jdrstart=base+"""jdrstart :
Use this to start a new JDR
"""
, jdrstop=base+"""jdrstop :
Use this to stop the JDR and receive the data
"""
, galnet=base+"""galnet [index] :
Used to get RSS feed from GalNet forums. Use the index to see a specific news or let it be random
index : number of the news wanted"""
, wiki=base+"""wiki [word] :
When you are too lazy to surf on the internet by yourself."""
, pict="Kill me please... Don't you read the help ? Oh..."
, yt = base+"""yt [-s|-p] [link|index] [title] :
Youtube function to have some videos on the channel !
-s : Option to save the link (need the link in the command line)
-p : Will play the selected video
link : link to a YouTube video
index : index of a saved link
title : title of the saved link (need the -s option)"""
, quote=base+"""quote [s] [quote|index] :
Used to quote your friends when they are funny or to remember a funny quote
-s : Used to save a quote (need the quote in the command line)
quote : quote that will be saved
index : index of a saved quote""")
global datadir
datadir = os.path.dirname("./data/")
if not os.path.exists(datadir):
os.makedirs(datadir)
##Program
# bool isSetUp() : attempts to open setup.txt. Returns true if opening is successful, otherwise returns false.
def isSetUp(file):
try:
setupFile=open(file,"r")
return True
except:
return False
def readToken():
tokenFile=open(datadir+"/token.txt", "r")
token = tokenFile.readline()
tokenFile.close()
return token
def createToken():
token=input("Please enter you bot's token : ")
try:
tokenFile=open(datadir+"/token.txt", "w")
tokenFile.write(token)
tokenFile.close()
except:
print("An error occured. Shutting down...")
print("Token registered.")
def logMessage(message):
dir = os.path.dirname("./logs/"+message.server.name+"/")
if not os.path.exists(dir):
os.makedirs(dir)
today = datetime.datetime.now()
logsFile=open("./logs/" + message.server.name + "/" + str(today.day) + "-" + str(today.month) + "-" + str(today.year) + ".txt","a")
log = str(today.hour) + ":" + str(today.minute) + ":" + str(today.second) + " [" + str(message.author.name) + "@" +message.server.name + "." + message.channel.name + "] : " + message.content + "\n"
logsFile.write(log)
print(log,end='')
def sLogs(message):
dir = os.path.dirname("./superlogs/"+message.server.name+"/")
if not os.path.exists(dir):
os.makedirs(dir)
today = datetime.datetime.now()
logsFile=open("./superlogs/" + message.server.name + "/" + str(today.day) + "-" + str(today.month) + "-" + str(today.year) + ".txt","a")
log = str(today.hour) + ":" + str(today.minute) + ":" + str(today.second) + " [" + str(message.author.name) + "@" +message.server.name + "." + message.channel.name + "] : " + message.content + "\n"
logsFile.write(log)
def logStart():
dir = os.path.dirname("./logs/")
if not os.path.exists(dir):
os.makedirs(dir)
today = datetime.datetime.now()
logsFile=open("./logs/startup.txt","a")
log="[" + str(today) + "] : Bot " + __program__ + " v" + __version__ + " logged in as " + client.user.name + " \n"
logsFile.write(log)
print(log,end='')
def rssFeed(index=None):
python_wiki_rss_url = "https://community.elitedangerous.com/galnet-rss"
feed = feedparser.parse( python_wiki_rss_url )
entries = []
for i in range(len(feed["items"])):
if "Weekly" not in feed["items"][i]["title"]:
entries.append( feed[ "items" ][i] )
if index == None:
index = random.randrange(len(entries))
try:
return entries[index]
except:
return None
def ytSave(link, title):
ytFile = open (datadir+"/yt.txt", "a")
ytFile.write(title + " " + link + "\n")
print("Saved link "+ link)
ytFile.close()
return "Saved link n°"+ str(fileSize(datadir+"/yt.txt"))
def ytRead(pos):
ytFile = open(datadir+"/yt.txt" , "r")
ytList = ytFile.readlines()[int(pos)-1].strip("\n")
#print (ytList)
ytFile.close()
return ytList
def ytPlay(target):
if target.isdigit():
print('Playing video from list : ' + target + ' +link')
msg = 'Playing video from list : ' + target + '\n'
ytlink = ytRead(target)
msg += ytlink.split(' ')[1]
print(ytlink.split(' ')[1])
else:
msg = 'Playing video from YouTubeApi : ' + target
print('Playing video from YouTubeApi : ' + target)
return msg
def qtSave(quote):
qtFile = open (datadir+"/quote.txt", "a")
qtFile.write(quote + "\n")
print("Saved quote "+ quote)
qtFile.close()
return "Saved quote n°"+ str(fileSize(datadir+"/quote.txt")) + " : " + quote
def qtRead(index):
qtFile = open(datadir+"/quote.txt" , "r")
qtList = qtFile.readlines()[int(index)-1].strip("\n")
qtFile.close()
return qtList
def fileSize(filename):
file = open(filename, 'r')
num = len(file.readlines())
file.close()
return num
def newDice(players,player,jet,sur):
try:
players[player].append(jet/sur*100)
except KeyError:
players[player] = [jet/sur*100]
## Starting the bot!
if isSetUp(datadir+"/setup.txt")==False:
setup.setup_common()
setupArray=setup.start()
else:
setupArray=setup.start()
if isSetUp(datadir+"/token.txt")==False:
createToken()
## The bot itself
global client
client = discord.Client()
@client.event
@asyncio.coroutine
def on_ready():
#lel=open("sorataisbored.png","rb")
#yield from client.edit_profile(avatar=lel.read())
if setupArray[0]=='0':
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
elif setupArray[0]=='1':
print('Connecté en tant que')
print(client.user.name)
print(client.user.id)
print('------')
logStart()
## Other functions
@client.event
@asyncio.coroutine
def Sendmessage(msg,message):
yield from client.send_message(message.channel, msg)
logMessage(message)
jdrJets = dict() #For Roleplays
## Starting the coroutines
@client.event
@asyncio.coroutine
def on_message(message):
# Logs stuff
today = datetime.datetime.now()
logsFile=open("./logs/" + str(today.day) + "-" + str(today.month) + "-" + str(today.year) + ".txt","a")
if (setupArray[5]=="1"):
try:
sLogs(message)
except:
pass
lowerContent = message.content.lower
## Normal functions
#TODO: quizz, pic, quote
## A test function
if message.content.startswith(base+'hello') or ("Hello" in message.content and message.mentions[0].id==client.user.id):
hellos=["Hello", "Hi", "Hello CMDR", "Greetings", "Welcome", "Oh ! Hi"]
msg = hellos[random.randrange(len(hellos))] + ' {0.author.mention} !'.format(message)
yield from client.send_message(message.channel, msg)
logMessage(message)
## Another test function
if message.content.startswith(base+'version'):
msg = __program__ + ' is version v' + __version__ + '. '
yield from client.send_message(message.channel, msg)
logMessage(message)
# Still indev
if message.content.startswith('!pic'):
msg = '{0.author.mention} Sorry, but !pic is still indev.'.format(message)
yield from client.send_message(message.channel, msg)
logMessage(message)
if message.content.startswith('!quizz'):
msg = '{0.author.mention} Sorry, but !quizz is still indev.'.format(message)
yield from client.send_message(message.channel, msg)
logMessage(message)
if message.content.startswith('!quote'):
msg = '{0.author.mention} Sorry, but !quote is still indev.'.format(message)
yield from client.send_message(message.channel, msg)
logMessage(message)
## Wiki
if message.content.startswith('!wiki') and setupArray[0]=='0':
messageArray = message.content.split()
messageArray.remove(messageArray[0])
toSearch = '_'.join(messageArray)
msg = ('{0.author.mention} https://en.wikipedia.org/wiki/' + toSearch).format(message)
yield from client.send_message(message.channel, msg)
logMessage(message)
elif message.content.startswith('!wiki') and setupArray[0]=='1':
messageArray = message.content.split()
messageArray.remove(messageArray[0])
toSearch = '_'.join(messageArray)
msg = ('{0.author.mention} https://fr.wikipedia.org/wiki/' + toSearch).format(message)
yield from client.send_message(message.channel, msg)
logMessage(message)
## Help Function
if message.content.startswith(base+'help') or (message.content.startswith('Help') and message.mentions[0].id==client.user.id):
msg = "{0.author.mention}".format(message) + " Hey ! I'm " + client.user.name + " ! " + helptext
args = message.content.split(" ")
try:
if args[1] in functions:
msg = 'Usage : \n' + helpdict[args[1]]
elif args[1]=='all':
msg = 'All the definitions : \n'
for defs in functions:
msg += '```\n' + helpdict[defs] + '```\n'
else:
msg = "WTF is this function ?\n"+str(functions)+" that's all I have !"
except IndexError:
msg = "{0.author.mention}".format(message) + " Hey ! I'm " + client.user.name + " ! " + helptext
yield from client.send_message(message.channel, msg)
logMessage(message)
# Other functions
## RSS feed
if message.content.startswith(base+'galnet'):
var = message.content.split(" ")
try :
var= var[1]
var = int(float(var))
except Exception as error:
print (error)
var=None
feed = rssFeed(var)
if feed == None:
msg = "No RSS feed loaded. Maybe change the index"
else:
msg = "News from GalNet : #" + feed["title"] + "#\n```html\n" + re.sub('<[^<]+?>', '',feed["summary"]) + "```"
yield from client.send_message(message.channel, msg)
logMessage(message)
## JdrStart
if message.content.startswith(base+'jdrstart'):
jdrJets = dict()
msg = '```A new JDR started !```'
yield from client.send_message(message.channel, msg)
## Roll
if message.content.startswith(base+'roll '):
global jdrJets
raw = message.content.split(base+'roll ')[1]
mg = message
try:
yield from client.delete_message(message)
except:
pass
if list(raw)[0].isdigit() and list(raw)[1] == "d":
var = raw.split("d")
msg = "{0.author.mention} rolled ".format(mg)+raw
for i in range(int(var[0])):
dice = int(var[1])
res = random.randrange(int(var[1]))+1
msg += "\n It's a "+str(res)+" !"
try:
newDice(jdrJets,mg.author.name,res, dice)
except:
print("No jdrJets set up")
yield from client.send_message(mg.channel, msg)
logMessage(mg)
##JdrStop
if message.content.startswith(base+'jdrstop'):
global jdrJets
msg = "Resultats : \n```\n"
for key in jdrJets:
msg+=key
for k in jdrJets[key]:
msg+=","+str(k)
msg+="\n"
msg += '```\n FIN ! '
logMessage(message)
yield from client.send_message(message.author, msg)
yield from client.send_message(message.channel, "```FIN DU JDR```")
## Private Roll
if message.content.startswith(base+'pvroll '):
raw = message.content.split(base+'pvroll ')[1]
mg= message
try:
yield from client.delete_message(message)
except:
pass
if list(raw)[0].isdigit() and list(raw)[1] == "d":
var = raw.split("d")
msg = "{0.author.mention} rolled ".format(mg)+raw
for i in range(int(var[0])):
msg += "\n It's a "+str(random.randrange(int(var[1]))+1)+" !"
yield from client.send_message(mg.author, msg)
logMessage(mg)
## Quote Function
if message.content.startswith(base+'quote'):
mess = message.content.strip()
args = mess.split()
#Wanna read a quote ?
if len(args)>=2:
if args[1].isdigit():
msg = 'Reading quote...\n'
try:
msg += qtRead(args[1]) + "\n"
except:
msg += 'No quote found. Maybe the index was too big'
#Or better ! A random quote !
elif args[1] == '-r':
msg = qtRead(random.randrange(fileSize(datadir+'/quote.txt')))
#Or wanna save one ?
else:
print('Saving quote')
msg = qtSave(mess.strip(base + 'quote '))
else:
print('No index specified')
msg = qtRead(random.randrange(fileSize(datadir+'/quote.txt')))
yield from client.send_message(message.channel, msg)
logMessage(message)
## Youtube Function
if message.content.startswith(base+'yt'):
mess = message.content.strip()
args = mess.split(" ")
#If the function have a link
if 'youtu' in mess:
#Wanna save ?
if args[1] == '-s':
try:
msg = ytSave(args[2], args[3])
except:
msg = ytSave(args[2], "NoTitle")
#Wanna play ?
elif args[1] == '-p':
msg = "Playing video " + args[2] + "\n"
msg += ytPlay(args[2])
#Or just want some informations about it ?
else:
msg = "Finding vidéo " + args[1] + " Oh wait... I can't yet... Sorry !"
#No link ?
else:
#Want to search in the file ?
if len(args) >= 2:
if args[1].isdigit():
try:
msg = ytRead(args[1])
except:
msg = "No link found"
# Maybe search for an onlie video ?
elif args[1].isalnum():
msg= "Research video online : " + args[1] + " But I can't do that yet... Sorry ! "
#Hum... Play from file ?
elif args[1] == '-p' and args[2].isdigit():
msg = "Playing video " + args[2] + '\n'
msg += ytPlay(args[2])
#"Something bad happend... Something very bad...
else:
print("error")
msg = "Something happend. I'm sure something happend. But dunno what. Sorry !"
else:
print('No index specified')
msg = ytRead(random.randrange(fileSize(datadir+'/yt.txt')))
yield from client.send_message(message.channel, msg)
logMessage(message)
logsFile.close()
try:
client.run(readToken())
except:
print("DLBot not started. There might be a connection error.\nShutting down...")
quit()