forked from Abhijith14/discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBUDDYAIBOT.py
421 lines (348 loc) · 13.7 KB
/
BUDDYAIBOT.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
import discord
import datetime
import pytz
import wikipedia
from googlesearch import search
from discord.ext import commands
from discord.utils import get
import youtube_dl
from gtts import gTTS
import json
import os
data = "Sorry"
keywords_basic = {"what'syourname": "Hi, My name is BUDDY. Its nice to meet you. What can i do for you ?",
"howareyou": "Great.. Thanks for asking. How may I help ?",
"whatisyourname": "Hi, My name is BUDDY. Its nice to meet you. What can i do for you ?",
"whatareyoudoing": "I am helping you. Ask me, if anything comes by...",
"heybuddy": "Hello. How are you ?",
"hibuddy": "Hello. How are you ?",
"hellobuddy": "Hi. how are you ?",
"iamfine": "Great! How can i help ?",
"good": "You are welcome..",
"hello": "Hi. How are you ?",
"hola": "Hi. How are you ?",
"hi": "Hello, How are you ?",
"hey": "Hello, How are you ?"}
keywords_wish = ["goodmorning", "goodafternoon", "goodevening", "goodnight"]
keywords_date = ["today", "date", "whatisthedate", "whatistoday", "whatisthemonth", "whatistheday", "whatistheyear", "month", "year"]
keywords_clock = ["whatisthetime", "time", "what'sthetime"]
def read_token():
with open('app.json') as f:
appData = json.load(f)
return appData['env']['TOKEN']['value']
def read_clientid():
with open('app.json') as f:
appData = json.load(f)
return appData['env']['CLIENT_ID']['value']
def read_audioChannelid():
with open('app.json') as f:
appData = json.load(f)
return appData['env']['AUDIOCHANNEL_ID']['value']
def read_ChannelList():
with open('app.json') as f:
appData = json.load(f)
return appData['env']['Channels_Valid']['value']
def read_GuildId():
with open('app.json') as f:
appData = json.load(f)
return appData['env']['GUILD_ID']['value']
def read_TimeZone():
with open('app.json') as f:
appData = json.load(f)
return appData['env']['TIMEZONE']['value']
tz = pytz.timezone(read_TimeZone())
token = read_token()
client = discord.Client()
client = commands.Bot(command_prefix="")
MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
DAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
global channel_audio
@client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
def get_today(text):
today_day = text[8:10]
today_month = text[5:7]
today_year = text[0:4]
day_week = today_day + " " + today_month + " " + today_year
today_month = MONTHS[int(today_month) - 1]
day_week = datetime.datetime.astimezone(tz).today().weekday()
end = "th of "
if int(today_day[1]) == 1:
end = "st of "
elif int(today_day[1]) == 2:
end = "nd of "
elif int(today_day[1]) == 3:
end = "rd of "
today = "It is " + DAYS[day_week] +", the "+ today_day + end + today_month + ", " + today_year
return today
def get_search(text):
try:
url = "https://www.google.com/search?q="
ans = []
i = 0
for j in search(text, tld='com', lang='en', num=5, start=0, stop=5, pause=2.0):
ans.append(j)
ans.pop(-1)
ans.append("Finished Looking. Need Anything else ?")
return ans
except:
print("Sorry. Couldnt Search for it.")
def get_wish(string):
now = datetime.datetime.now()
now = now.astimezone(tz)
curr_time = str(now)
curr_time = curr_time[11:16]
if string == "goodmorning":
if int(curr_time[:2]) < 12:
return "Good Morning, How can I help ?"
elif int(curr_time[:2]) >= 12 and int(curr_time[:2]) < 16:
return "Its Good Afternoon. How can I help ?"
elif int(curr_time[:2]) > 16 and int(curr_time[:2]) < 24:
return "Its Good Evening. How can I help ?"
elif string == "goodafternoon":
if int(curr_time[:2]) < 12:
return "Its Good Morning, How can I help ?"
elif int(curr_time[:2]) >= 12 and int(curr_time[:2]) < 16:
return "Good Afternoon. How can I help ?"
elif int(curr_time[:2]) > 16 and int(curr_time[:2]) < 24:
return "Its Good Evening. How can I help ?"
elif string == "goodevening":
if int(curr_time[:2]) < 12:
return "Its Good Morning, How can I help ?"
elif int(curr_time[:2]) >= 12 and int(curr_time[:2]) < 16:
return "Its Good Afternoon. How can I help ?"
elif int(curr_time[:2]) > 16 and int(curr_time[:2]) < 24:
return "Good Evening. How can I help ?"
elif string == "goodnight":
if int(curr_time[:2]) < 12:
return "Good Night, Sweet Dreams."
else:
return "Night Night..."
@client.command(pass_context=True, aliases=['j','joi'])
async def join(ctx):
global voice
global channel_audio
try:
channel = ctx.message.author.voice.channel
channel_audio = channel
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
await voice.disconnect()
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
print(f"The bot has connected to {channel}\n")
await ctx.send(f"```Joined {channel}```")
except:
print("Call Error")
await ctx.send("```You are not connected to voice!```")
@client.command(pass_context=True, aliases=['l','lea'])
async def leave(ctx):
global channel_audio
channel = channel_audio#ctx.message.author.voice.channel
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.disconnect()
print(f"Bot has left {channel}")
await ctx.send(f"```Left {channel}```")
else:
print("Already Left")
await ctx.send(f"```Not in {channel}```")
@client.command(pass_context=True, aliases=['p','pla'])
async def play(ctx, url: str):
song_there = os.path.isfile("song.mp3")
try:
if song_there:
os.remove("song.mp3")
print("Removed song")
except PermissionError:
print("Trying But Failed!!")
await ctx.send("```Error. Music Couldnt be played```")
return
await ctx.send("```Searching Youtube...```")
try:
global channel_audio
channel = client.get_channel(read_audioChannelid())
channel_audio = channel
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
await voice.disconnect()
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
print(f"The bot has connected to {channel}\n")
await ctx.send(f"```Joined {channel}```")
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192'
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
print("Downloading...")
ydl.download([url])
for file in os.listdir("./"):
if file.endswith(".mp3"):
name = file
print(f"Renaming {file}\n")
os.rename(file, "song.mp3")
voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e: print(f"{name} has finished playing"))
voice.source = discord.PCMVolumeTransformer(voice.source)
voice.source.volume = 501
nname = name.rsplit("-", 2)
await ctx.send(f"```playing {nname[0]} in channel {channel}```")
print("Playing\n")
except:
print("Call Error")
await ctx.send("```Try connecting to any voice channel...```")
@client.command(pass_context=True, aliases=['pa','pau'])
async def pause(ctx):
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..")
await ctx.send("```Cant Pause if nothing is playing```")
@client.command(pass_context=True, aliases=['r','res'])
async def resume(ctx):
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_paused():
print("Resuming Music")
voice.resume()
await ctx.send("```Resumed Music```")
else:
print("Music not Paused..")
await ctx.send("```Cant Resume if music is not played```")
@client.command(pass_context=True, aliases=['s', 'sto'])
async def stop(ctx):
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_playing():
print("Stopping Music")
voice.stop()
await ctx.send("```Music Stopped```")
else:
print("Music not Stopped..")
await ctx.send("```Cant Stop if music is not played```")
@client.command(pass_context=True, aliases=["search"])
async def google_search(ctx):
global data
f = "```Sure. Going Online for " + data[7:] + "```"
await ctx.send(f)
await ctx.send("```You Have Results from :```")
ans = get_search(data[7:])
for i in ans:
await ctx.send(i)
@client.command(pass_context=True, aliases=["define"])
async def wiki_search(ctx):
global data
try:
ind = 7
data = data[ind:]
dataW = wikipedia.summary(data)
j = -1
s = 0
for i in dataW:
j = j + 1
if '.' in i and s < 3:
s = s + 1
indexC = j
await ctx.send("```" + wikipedia.summary(data)[:indexC + 1] + "```")
except:
await ctx.send("```Sorry. Try using more words to enhance your experience...```")
@client.command(pass_context=True, aliases=["!users"])
async def member_count(ctx):
id = client.get_guild(read_GuildId())
await ctx.send(f"""```{id.member_count} Members ```""")
@client.command(pass_context=True, aliases=list(keywords_basic.keys()))
async def basic(ctx):
global data
question = list(keywords_basic.keys())
for phrase in question:
if phrase in data:
await ctx.send("```" + str(keywords_basic[phrase]) + "```")
@client.command(pass_context=True, aliases=keywords_wish)
async def basic_wish(ctx):
global data
for phrase in keywords_wish:
if phrase in data:
await ctx.send("```" + str(get_wish(phrase)) + "```")
@client.command(pass_context=True, aliases=keywords_date)
async def today_date(ctx):
global data
for phrase in keywords_date:
if phrase in data:
now = datetime.datetime.now()
now = now.astimezone(tz)
curr_date = str(now)
await ctx.send("```" + str(get_today(curr_date[0:10]))+ "```")
@client.command(pass_context=True, aliases=keywords_clock)
async def date_clock(ctx):
global data
for phrase in keywords_clock:
if phrase in data:
now = datetime.datetime.now()
now = now.astimezone(tz)
curr_time = str(now)
curr_time = curr_time[11:16]
if int(curr_time[:2]) < 12:
await ctx.send("```Its " + curr_time + " am```")
elif int(curr_time[:2]) > 12:
hours = int(curr_time[:2]) - 12
min = int(curr_time[3:])
now_time = str(hours) + ":" + str(min)
await ctx.send("```Its " + now_time + " pm```")
elif int(curr_time[:2]) == 12:
await ctx.send("```Its " + curr_time + " pm```")
@client.event
async def on_message(message):
channels = read_ChannelList()
if message.author.bot:
print(message.content)
elif str(message.channel) in channels and str(message.author):
bot_code = str(message.content)[:22]
msg_limit = 23
bc1 = "<@!" + str(read_clientid()) + ">"
bc2 = "<@" + str(read_clientid()) + "> "
if bot_code == bc1 or bot_code == bc2:
if bot_code == bc2:
msg_limit = 22
original = str(message.content)[msg_limit:]
com = original[0:4].lower()
music = original[4:]
google = original[0:6].lower()
message.content = original
if com == "play":
message.content = com + music
print(com + "Message " + message.content)
elif google == "search" or google == "define":
message.content = message.content.lower()
print(google + "Message " + message.content)
else:
message.content = message.content.lower().replace(" ", "")
print(google + "Message " + message.content)
global data
data = str(message.content)
await client.process_commands(message)
if data == "!delete":
await message.channel.purge(limit=125)
else:
print(bot_code)
print(f"""User: {message.author} talking {message.content}, in channel {message.channel}""")
# else:
# await message.channel.send("Invalid User!!")
# print(f"""User: {message.author} tried to do command {message.content}, in channel {message.channel}""")
client.run(token)
# print(read_ChannelList())