-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
400 lines (279 loc) · 11.6 KB
/
main.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
# the os module helps us access environment variables
# i.e., our API keys
import os, platform
try:
import nacl
except ImportError:
try:
if platform.system().lower().startswith('win'):
os.system("pip install pynacl")
else:
os.system("pip3 install pynacl")
except Exception as e:
print("Error:", e)
exit()
# these modules are for querying the Hugging Face model
import json
import requests
# the Discord Python API
import discord
from dotenv import load_dotenv
from discord.ext import commands
from discord.utils import get
from discord import FFmpegPCMAudio
from discord import TextChannel
from youtube_dl import YoutubeDL
from youtube_search import YoutubeSearch
import asyncio
# this is my Hugging Face profile link
API_URL = 'https://api-inference.huggingface.co/models/BlightZz/'
loop = False
class MyClient(commands.Bot):
def __init__(self, model_name, command_prefix):
super().__init__(command_prefix = "$")
self.command()(self.Hello)
self.command()(self.join)
self.command()(self.play)
self.command()(self.resume)
self.command()(self.stop)
self.command()(self.pause)
self.command()(self.hentai)
self.command()(self.commandlist)
self.command()(self.skip)
self.command()(self.leave)
self.command()(self.display)
self.command()(self.repeat)
self.command()(self.makePlaylist)
self.command()(self.addtoPlaylist)
self.command()(self.showPlaylists)
self.command()(self.playlist)
self.command()(self.devDisplay)
self.queue = []
self.queuedict = {}
with open("servers.json", "r") as json_file:
self.queuedict = json.load(json_file)
self.playlistdict = {}
with open("playlists.json", "r") as json_file:
self.playlistdict = json.load(json_file)
self.api_endpoint = API_URL + model_name
# retrieve the secret API token from the system environment
huggingface_token = os.environ['HUGGINGFACE_TOKEN']
# format the header in our request to Hugging Face
self.request_headers = {
'Authorization': 'Bearer {}'.format(huggingface_token)
}
def query(self, payload):
"""
make request to the Hugging Face model API
"""
data = json.dumps(payload)
response = requests.request('POST',
self.api_endpoint,
headers=self.request_headers,
data=data)
ret = json.loads(response.content.decode('utf-8'))
return ret
async def devDisplay(self, ctx):
await ctx.send(self.playlistdict)
async def on_ready(self):
# print out information when the bot wakes up
print('Logged in as')
print(self.user.name)
print(self.user.id)
print('------')
await self.change_presence(activity=discord.Game(name="$play URL, or @ me to chat!"))
# send a request to the model without caring about the response
# just so that the model wakes up and starts loading
self.query({'inputs': {'text': 'Hello!'}})
for guild in self.guilds:
self.queuedict[guild.id] = []
print(guild.id)
print("added to dict")
with open("servers.json", "w") as outfile:
json.dump(self.queuedict, outfile)
print("Saved server list as json.")
async def makePlaylist(self, ctx, *, content):
self.playlistdict[content] = []
async def playlist(self, ctx, *, content):
for s in self.playlistdict[content]:
try:
self.queuedict[ctx.guild.id].append(s)
except:
self.queuedict[ctx.guild.id] = [s]
await self.play(ctx, url = self.queuedict[ctx.guild.id].pop())
async def showPlaylists(self, ctx):
temp = "```These are the currently available playlists and their content.\n"
count = 1
for key, value in self.playlistdict.items():
temp = temp + key + ":" + "\n"
count = 1
for s in value:
temp = temp + str(count) + ". " + s + "\n"
count = count + 1
temp = temp + "```"
await ctx.send(temp)
async def addtoPlaylist(self, ctx, *, content):
targetPlaylist = content[0:content.find(',')]
song = content[content.find(',')+2: len(content)]
self.playlistdict[targetPlaylist].append(song)
with open("playlists.json", "w") as outfile:
json.dump(self.playlistdict, outfile)
message = "Added " + "*" + song + "*" " to playlist " + "*" + targetPlaylist + "*"
await ctx.send(message)
async def repeat(self, ctx):
global loop
loop = not loop
if loop:
await ctx.send("I'll loop the song.")
else:
await ctx.send("I'll stop looping the song")
async def on_message(self, message):
# ignore the message if it comes from the bot itself
if message.author.id == self.user.id:
return
# ignore if not @
if "fuck" in message.content:
emoji = discord.utils.get(message.guild.emojis, name = 'KurisuPout')
await message.add_reaction(emoji)
if "vamo" in message.content:
emoji = discord.utils.get(message.guild.emojis, name = 'OKabe')
await message.add_reaction(emoji)
if "kurisu" in message.content:
emoji = discord.utils.get(message.guild.emojis, name = 'KurisuCute')
await message.add_reaction(emoji)
if "sus" in message.content:
emoji = discord.utils.get(message.guild.emojis, name = 'Amadeus')
await message.add_reaction(emoji)
if "ostia" in message.content:
emoji = discord.utils.get(message.guild.emojis, name = 'ostia')
await message.add_reaction(emoji)
if not self.user.mentioned_in(message):
await self.process_commands(message)
return
# form query payload with the content of the message
payload = {'inputs': {'text': message.content}}
# while the bot is waiting on a response from the model
# set the its status as typing for user-friendliness
async with message.channel.typing():
response = self.query(payload)
bot_response = response.get('generated_text', None)
# we may get ill-formed response if the model hasn't fully loaded
# or has timed out
if not bot_response:
if 'error' in response:
bot_response = '`Error: {}`'.format(response['error'])
else:
bot_response = 'No use talking anymore.'
# send the model's response to the Discord channel
await message.channel.send(bot_response)
async def display(self, ctx):
count = 1
temp = "```These are the songs currently in the queue:\n"
for s in self.queuedict[ctx.guild.id]:
temp = temp + str(count) + ". " + s + "\n"
count = count + 1
temp = temp + "```"
await ctx.send(temp)
async def Hello(self, ctx):
await ctx.send("Hello!")
async def join(self, ctx):
channel = ctx.message.author.voice.channel
voice = get(self.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
async def play(self, ctx,*, url):
print("play called")
await self.join(ctx)
message = await ctx.send('Working on it...')
FFMPEG_OPTIONS = {'before_options' : '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options' : '-vn'} #youtube_dl options
YDL_OPTIONS = {'format': 'bestaudio/best', 'extractaudio': True, 'audioformat': 'mp3', 'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
'restrictfilenames': True, 'noplaylist': True, 'nocheckcertificate': True, 'ignoreerrors': False, 'logtostderr': False, 'quiet': True,
'no_warnings': True, 'default_search': 'auto', 'source_address': '0.0.0.0'}
voice = get(self.voice_clients, guild=ctx.guild)
#Youtube stuff
if not voice.is_playing():
with YoutubeDL(YDL_OPTIONS) as ydl:
results = YoutubeSearch(url, max_results=1).to_dict()
#print(str(results))
url2 = results[0]['id']
#print(url2)
info = ydl.extract_info(url2, download=False)
URL = info['url']
voice.play(FFmpegPCMAudio(URL, **FFMPEG_OPTIONS, ), after= lambda e: asyncio.run_coroutine_threadsafe(self.play_next(ctx, url), self.loop).result())
#Auxiliary Message Handling
voice.is_playing()
await message.edit(content = 'Enjoy your music!\n'+ 'https://www.youtube.com/watch?v='+url2)
emoji = discord.utils.get(ctx.guild.emojis, name = 'KurisuThumbsUp')
emoji2 = discord.utils.get(ctx.guild.emojis, name = 'KurisuHeart')
await message.add_reaction(emoji)
await message.add_reaction(emoji2)
else:
try:
self.queuedict[ctx.guild.id].append(url)
except:
self.queuedict[ctx.guild.id] = [url]
await ctx.message.delete()
await ctx.send("I added your song to the queue.")
async def play_next(self, ctx, url):
if loop:
await self.play(ctx = ctx, url = url)
print("play_next called")
if len(self.queuedict[ctx.guild.id]) >= 1:
print("play_next if success.")
await self.play(ctx = ctx, url = self.queuedict[ctx.guild.id].pop(0))
else:
print("play_next else fulfilled")
return
async def leave(self, ctx):
await ctx.message.guild.voice_client.disconnect()
await ctx.send("See you later.")
async def skip(self, ctx):
if len(self.queuedict[ctx.guild.id]) >= 1:
print("skip if success.")
voice = get(self.voice_clients, guild=ctx.guild)
if voice.is_playing():
voice.stop()
await ctx.send("Skipping.")
else:
ctx.send("What do you want me to skip? There's nothing playing.")
else:
voice = get(self.voice_clients, guild=ctx.guild)
voice.stop()
await ctx.send("The queue is empty now.")
async def resume(self, ctx):
voice = get(self.voice_clients, guild=ctx.guild)
if not voice.is_playing():
voice.resume()
await ctx.send('Resuming.')
# check if the bot is already playing
else:
await ctx.send("Resume what? The music is already playing.")
return
async def pause(self, ctx):
voice = get(self.voice_clients, guild=ctx.guild)
if voice.is_playing():
voice.pause()
await ctx.send('Paused.')
else:
await ctx.send("Pay attention, there's nothing playing.")
async def stop(self, ctx):
voice = get(self.voice_clients, guild=ctx.guild)
self.queue.clear()
if voice.is_playing():
voice.stop()
await ctx.send('Stopped.')
else:
await ctx.send("Stop what? Seriously, pay attention. There's nothing playing.")
async def hentai(self, ctx):
await ctx.channel.send(file=discord.File('Horny.gif'))
async def commandlist(self, ctx):
await ctx.channel.send('Hey! These are the commands available right now:\n $join\n$play\n$resume\n$pause\n$stop\n$hentai\n\nYou can also @ me if you want to chat!')
def main():
#DialoGPT-medium-Kurisu is my model name
client = MyClient('DialoGPT-medium-Kurisu', '$')
client.run(os.environ['DISCORD_TOKEN'])
if __name__ == '__main__':
main()
my_secret = os.environ['DISCORD_TOKEN']