-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
74 lines (63 loc) · 2.25 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
import asyncio
import discord
import queue
import sys
import threading
from config import DISCORD_KEY, DISCORD_CHANNEL
if not (DISCORD_KEY and DISCORD_CHANNEL):
print("Please set DISCORD_KEY and DISCORD_CHANNEL in config.py")
sys.exit(1)
class Bot:
def __init__(self, token, vc_channel):
self.client = discord.Client()
self.queue = queue.Queue(maxsize=10)
self.channel_id = vc_channel
self.token = token
self.ready = False
def run(self):
self.cons_loop = asyncio.new_event_loop()
self.cons_loop.create_task(self.consume())
threading.Thread(target=self.cons_loop.run_forever).start()
self.client.event(self.on_message)
self.client.event(self.on_ready)
self.client.run(self.token)
async def on_ready(self):
chan = self.client.get_channel(self.channel_id)
self.vc = await self.client.join_voice_channel(chan)
self.ready = True
print("Bot is ready.")
async def on_message(self, message):
print("Message recived: {}".format(message.content))
if not self.ready:
print("Ignoring message")
return
tok = message.content.split(" ")
if tok[0] == "]]":
if tok[1] == "skip":
print("skipping")
if self.player:
self.player.stop()
else:
try:
self.queue.put(tok[1], block=False)
print("Added {} to queue.".format(tok[1]))
except queue.Full:
await self.client.send_message(
message.channel, "Sorry the queue is already full."
)
async def consume(self):
asyncio.set_event_loop(self.cons_loop)
try:
while True:
it = self.queue.get()
print("Playing " + it)
self.player = self.vc.create_ytdl_player(it)
print("player started for {}".format(it))
self.player.start()
self.player.join()
print("player ended for {}".format(it))
except Exception as e:
print(e)
if __name__ == "__main__":
b = Bot(DISCORD_KEY, DISCORD_CHANNEL)
b.run()