-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
650 lines (533 loc) · 19.4 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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
import discord
import random
import os
import youtube_dl
import wikipedia
from discord.ext import commands, tasks
from discord.utils import get
from itertools import cycle
import sqlite3
from datetime import datetime
import time
import database
import json
import re
from database import *
import pkg_resources
import asyncio
import aiml
client = commands.Bot(command_prefix='*')
status = cycle(['Scrabble', 'Chess'])
players = {}
# AI Chatbot
STARTUP_FILE = "std-startup.xml"
aiml_kernel = aiml.Kernel()
if os.path.isfile("bot_brain.brn"):
aiml_kernel.bootstrap(brainFile="bot_brain.brn")
else:
aiml_kernel.bootstrap(learnFiles="std-startup.xml", commands="load aiml b")
aiml_kernel.saveBrain("bot_brain.brn")
@client.command()
async def ask(ctx, *, question):
'''
- opens ai chatbot
'''
if question is None:
print("Empty message received.")
return
print("Message: " + str(question))
aiml_response = aiml_kernel.respond(question)
if aiml_response == '':
await ctx.send("I don't have a response for that, sorry.")
else:
print(aiml_response)
await ctx.send(aiml_response)
@client.event
async def on_ready():
# activity=discord.Game('Scrabble'))
await client.change_presence(status=discord.Status.idle)
change_status.start()
print("Bot is ready!")
@client.event
async def on_member_join(member):
print(f'{member} has joined a server.')
@client.event
async def on_member_remove(member):
print(f'{member} has left a server.')
@client.command()
async def ping(ctx):
'''
- Returns the Bot's response time.
'''
await ctx.send(f'Pong! {round(client.latency * 1000)}ms')
# general error handler
@client.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandNotFound):
pass
# await ctx.send('Invalid command used.')
@client.command()
@commands.has_permissions(manage_messages=True)
async def clear(ctx, amount: int):
'''
- Delete specified number of messages.
'''
await ctx.channel.purge(limit=amount)
# specific error handler
@clear.error
async def clear_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send('Please specify an amount of messages to delete.')
@client.command()
# @commands.check(is_it_me)
async def user(ctx):
'''
- Returns the username.
'''
await ctx.send(f'Hi! I am {ctx.author}')
@client.command()
async def kick(ctx, member: discord.Member, *, reason=None):
'''
- Kick members from the server.
'''
await member.kick(reason=reason)
await ctx.send(f'Kicked {member.mention}')
@client.command()
async def ban(ctx, member: discord.Member, *, reason=None):
'''
- Ban members from the server.
'''
await member.ban(reason=reason)
await ctx.send(f'Banned {member.mention}')
@client.command()
async def unban(ctx, *, member):
'''
- Unban banned members from the server.
'''
banned_users = await ctx.guild.bans()
member_name, member_discriminator = member.split('#')
for ban_entry in banned_users:
user = ban_entry.user
if (user.name, user.discriminator) == (member_name, member_discriminator):
await ctx.guild.unban(user)
await ctx.send(f'Unbanned {user.name}#{user.discriminator}')
# same as ctx.send(f'Banned {user.mention}')
return
# Magic 8Ball
@client.command(aliases=['8ball'])
async def _8ball(ctx, *, question):
'''
- Magic 8-Ball trivia.
'''
responces = ['It is certain.',
'It is decidedly so.',
'Without a doubt.',
'Yes – definitely.',
'You may rely on it.',
'As I see it, yes.',
'Most likely.',
'Outlook good.',
'Yes.',
'Signs point to yes.',
'Reply hazy, try again.',
'Ask again later.',
'Better not tell you now.',
'Cannot predict now.',
'Concentrate and ask again.',
"Don't count on it.",
'My reply is no.',
'My sources say no.',
'Outlook not so good.',
'Very doubtful.']
await ctx.send(f':8ball:Question: {question}\n:8ball:Answer: {random.choice(responces)}')
# Cogs Functions
@client.command()
async def load(ctx, extension):
'''
- Load the mentioned cogs file.
'''
client.load_extension(f'cogs.{extension}')
await ctx.send(f'{extension} Loaded')
@client.command()
async def unload(ctx, extension):
'''
- Unload the mentioned cogs file.
'''
client.unload_extension(f'cogs.{extension}')
await ctx.send(f'{extension} Unloaded')
@client.command()
async def reload(ctx, extension):
'''
- Reload the mentioned cogs file.
'''
client.unload_extension(f'cogs.{extension}')
client.load_extension(f'cogs.{extension}')
await ctx.send(f'{extension} Reloaded')
# Music Player Function
@client.command(name='join', help='- Join the voice channel.')
async def join(ctx):
if not ctx.message.author.voice:
await ctx.send("You are not connected to a voice channel")
return
else:
channel = ctx.message.author.voice.channel
print(f"The bot has connected to {channel}\n")
await channel.connect()
@client.command(pass_context=True)
async def leave(ctx):
'''
- Disconnect from the voice channel.
'''
channel = ctx.message.author.voice.channel
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.disconnect()
print(f"The bot has left {channel}")
await ctx.send(f"Left {channel}")
else:
print("Bot was told to leave voice channel, but was not in one")
await ctx.send("Don't think I am in a voice channel")
@client.command(pass_context=True)
async def play(ctx, url):
'''
- Play YouTube audio using URL.
'''
song_there = os.path.isfile("song.mp3")
try:
if song_there:
os.remove("song.mp3")
print("Removed old song file")
except PermissionError:
print("Trying to delete song file, but it's being played")
await ctx.send("ERROR: Music playing")
return
await ctx.send("Getting everything ready now")
voice = get(client.voice_clients, guild=ctx.guild)
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
print("Downloading audio now\n")
ydl.download([url])
for file in os.listdir("./"):
if file.endswith(".mp3"):
name = file
print(f"Renamed File: {file}\n")
os.rename(file, "song.mp3")
voice.play(discord.FFmpegPCMAudio("song.mp3"),
after=lambda e: print("Song done!"))
voice.source = discord.PCMVolumeTransformer(voice.source)
voice.source.volume = 0.1
nname = name.rsplit("-", 2)
await ctx.send(f"Playing: {nname[0]}")
print("playing\n")
@client.command(pass_context=True)
async def pause(ctx):
'''
- Pause the playing audio.
'''
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_playing():
print("Music paused")
voice.pause()
await ctx.send("Music paused")
else:
print("Music not playing failed pause")
await ctx.send("Music not playing failed pause")
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
client.load_extension(f'cogs.{filename[:-3]}')
@client.command(pass_context=True)
async def resume(ctx):
'''
- Resume the paused audio.
'''
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_paused():
print("Resumed music")
voice.resume()
await ctx.send("Resumed music")
else:
print("Music is not paused")
await ctx.send("Music is not paused")
@client.command(pass_context=True)
async def stop(ctx):
'''
- Stop the playing audio.
'''
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_playing():
print("Music stopped")
voice.stop()
await ctx.send("Music stopped")
else:
print("No music playing failed to stop")
await ctx.send("No music playing failed to stop")
# Leaderboard Function
@client.event
async def on_reaction_add(reaction, user):
if(check_leaderboard(reaction.message.id, user.id)):
if(reaction.emoji == u"\u25B6"):
page, last_user_count = get_leaderboard_page(
reaction.message.id, user.id)
if(last_user_count < page * 10):
return
rows = get_users(page+1)
embed = discord.Embed(title="Leaderboard", color=0x8150bc)
for row in rows:
if(row[1] != None and row[2] != None):
user_name = client.get_user(int(row[1]))
user_name = "#" + str(last_user_count) + \
" | " + str(user_name)
embed.add_field(
name=user_name, value='{:,}'.format(row[2]), inline=False)
last_user_count += 1
update_leaderboard(page + 1, last_user_count, reaction.message.id)
await reaction.message.edit(embed=embed)
await reaction.message.clear_reactions()
await reaction.message.add_reaction(u"\u25C0")
if(last_user_count > (page+1) * 10):
await reaction.message.add_reaction(u"\u25B6")
if(reaction.emoji == u"\u25C0"):
page, last_user_count = get_leaderboard_page(
reaction.message.id, user.id)
if(page == 1):
return
rows = get_users(page-1)
embed = discord.Embed(title="Leaderboard", color=0x8150bc)
if(last_user_count <= page * 10):
last_user_count -= 10 + (last_user_count-1) % 10
else:
last_user_count -= 20
for row in rows:
if(row[1] != None and row[2] != None):
user_name = client.get_user(int(row[1]))
user_name = "#" + str(last_user_count) + \
" | " + str(user_name)
embed.add_field(
name=user_name, value='{:,}'.format(row[2]), inline=False)
last_user_count += 1
update_leaderboard(page - 1, last_user_count, reaction.message.id)
await reaction.message.edit(embed=embed)
await reaction.message.clear_reactions()
if(page - 1 > 1):
await reaction.message.add_reaction(u"\u25C0")
await reaction.message.add_reaction(u"\u25B6")
if(reaction.emoji == u"\U0001F44D"):
roles = user.roles
permission = False
for role in roles:
if(role.name == "Manager" or role.permissions.administrator):
permission = True
if(permission and check_requests(reaction.message.id) and not user.bot):
users, points = get_users_requests(reaction.message.id)
split_users = users.split()
for user_id in split_users:
add_points(user_id, points)
update_requests(reaction.message.id, 1)
await reaction.message.add_reaction('\U00002705')
@client.command(pass_context=True)
async def points(ctx, command=None, username=None, point=None):
"""- Add and remove points."""
# print(username)
if(command == None or point == None or username == None):
if(command == None and point == None and username == None):
points = get_user_point(ctx.message.author.id)
await ctx.send("You have " + str(points) + " points")
return
else:
await ctx.send("Invalid command, please check the documentation: \n!points [add/remove] <username> <points>")
return
roles = ctx.message.author.roles
permission = False
for role in roles:
if(role.name == "Manager" or role.permissions.administrator):
permission = True
if(not permission):
await request_points(ctx)
# await ctx.send("No permission")
return
if(command.lower() == "add"):
if(point.isdigit()):
username_id = username[2:]
username_id = username_id[:-1]
username_id = username_id.replace("!", "")
if(username_id.isdigit()):
add_points(username_id, point)
else:
from_server = ctx.guild
user = from_server.get_member_named(username)
if(user == None):
await ctx.send("Invalid user")
return
else:
add_points(user.id, point)
await ctx.send("Points added!")
else:
await request_points()
else:
if(command.lower() == "remove"):
if(point.isdigit()):
username_id = username[2:]
username_id = username_id[:-1]
username_id = username_id.replace("!", "")
if(username_id.isdigit()):
remove_points(username_id, point)
else:
from_server = ctx.guild
user = from_server.get_member_named(username)
if(user == None):
await ctx.send("Invalid user")
return
else:
remove_points(user.id, point)
await ctx.send("Points removed!")
else:
await ctx.send("Invalid points number!")
else:
await ctx.send("Invalid command, please check the documentation: \n!points [add/remove] <username> <points>")
@client.command(pass_context=True)
async def leaderboard(ctx):
"""- Displays the score and rank based on points system."""
rows = get_users(1)
embed = discord.Embed(title="Leaderboard", color=0x8150bc)
count = 1
for row in rows:
if(row[1] != None and row[2] != None):
user = client.get_user(int(row[1]))
user = "#" + str(count) + " | " + str(user)
embed.add_field(
name=user, value='{:,}'.format(row[2]), inline=False)
count += 1
msg_sent = await ctx.send(embed=embed)
add_leaderboard(ctx.message.author.id, msg_sent.id, count)
if(count == 11):
await msg_sent.add_reaction(u"\u25B6")
@client.event
async def on_message_edit(before, after):
if(check_requests(after.id)):
update_requests(after.id, -1)
@client.event
async def on_command_error(ctx, error):
try:
await request_points(ctx)
except Exception as e:
print("Some shit happened: " + str(error))
print("Error from try catch : " + str(e))
@client.command(pass_context=True)
async def reset(ctx):
"""- Resets the database."""
permission = False
roles = ctx.message.author.roles
for role in roles:
if(role.permissions.administrator):
permission = True
if(permission):
await reset_database()
await ctx.send("Database was reset!")
else:
await ctx.send("No permision!")
async def format_user(user_name):
for i in range(len(user_name)):
if(user_name[i] != ' '):
break
else:
user_name = user_name[1:]
for i in user_name[::-1]:
if(i != " "):
break
else:
user_name = user_name[:-1]
return user_name
async def request_points(ctx):
#print("here it go")
message_sent = ctx.message.content
if(message_sent[:12] == "*points add "):
message_sent = message_sent[12:]
split_message = re.split('\s+', message_sent)
users = ''
# print(split_message)
for i in range(0, len(split_message) - 1):
users += split_message[i]
users += ' '
users = users[:-1]
# print(users)
split_users = users.split(',')
saved_users = ''
# print(split_users)
for user in split_users:
user = await format_user(user)
# print(user)
if(user[:1] == '"' and user[-1:] == '"'):
user = user[1:]
user = user[:-1]
# print(user)
user_id = ctx.guild.get_member_named(user)
if(user_id == None):
await ctx.send("The following user does not exist: " + str(user) + "\nPlease do not use white spaces between users and commas")
return
saved_users += str(user_id.id)
saved_users += ' '
elif(user[:1] == "<"):
user = user.strip()
user = user[2:]
user = user[:-1]
user = user.replace("!", "")
if(user.isdigit()):
found = client.get_user(int(user))
else:
await ctx.send("The following user does not exist : " + str(user) + "\nPlease use comma between users!")
return
if(found == None):
await ctx.send("The following user does not exist : " + str(user) + "\nPlease use comma between users!")
return
saved_users += str(user)
saved_users += ' '
elif(user[-1:] == ">"):
user = user.strip()
user = user[2:]
user = user[:-1]
user = user.replace("!", "")
if(user.isdigit()):
found = client.get_user(int(user))
else:
await ctx.send("The following user does not exist : " + str(user) + "\nPlease use comma between users!")
return
found = client.get_user(user)
if(found == None):
await ctx.send("The following user does not exist : " + str(user) + "\nPlease use comma between users!")
return
saved_users += str(user)
saved_users += ' '
else:
# print("Ja")
user_id = ctx.guild.get_member_named(user)
if(user_id == None):
await ctx.send("The following user does not exist : " + str(user))
return
saved_users += str(user_id.id)
saved_users += ' '
insert_points_requests(ctx.message.id, saved_users, split_message[len(
split_message) - 1], 0, ctx.message.author.id)
roles = ctx.message.author.roles
permission = False
for role in roles:
if(role.name == "Manager" or role.permissions.administrator):
permission = True
if(not permission):
await ctx.message.add_reaction(u"\U0001F44D")
else:
users_req = saved_users.split()
for user in users_req:
add_points(user, split_message[len(split_message) - 1])
await ctx.send("Points added")
@tasks.loop(seconds=200)
async def change_status():
await client.change_presence(activity=discord.Game(next(status)))
# Insert Your Token Here (Discord Developer Portal: https://discord.com/developers/applications)
client.run("")