-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
138 lines (102 loc) · 3.07 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
from discord.ext import commands
from discord import FFmpegPCMAudio
from manager import Manager
from custom_queue import CustomQueue
from settings import DISCORD_TOKEN, GUILD_NAME
manager = Manager()
queues = CustomQueue()
bot = commands.Bot(command_prefix="!")
@bot.event
async def on_ready():
print(f"{bot.user.name} has connected to Discord!")
def find_voice(ctx):
voice = None
for voice_client in bot.voice_clients:
if voice_client.guild == ctx.guild:
voice = voice_client
return voice
async def start_playing(ctx, audio_name):
if audio_name == None:
return
def play(id, voice):
if not queues.get_loop(id):
queues.pop(id)
if queues.get_len(id) == 0:
return
source = FFmpegPCMAudio(queues.front(id))
player = voice.play(source, after=lambda x=None: play(id, voice))
id = ctx.message.guild.id
if ctx.author.voice:
channel = ctx.message.author.voice.channel
voice = find_voice(ctx)
if not voice:
voice = await channel.connect()
if voice.is_playing():
stop(ctx)
source = FFmpegPCMAudio(audio_name)
player = voice.play(source, after=lambda x=None: play(id, voice))
else:
await ctx.send("You are not on audio server")
@bot.command(name="leave")
async def leave(ctx):
id = ctx.message.guild.id
queues.set_loop(id, False)
queues.clear(id)
if ctx.voice_client:
await ctx.guild.voice_client.disconnect()
await ctx.send("I left the voice channel")
else:
await ctx.send("I am not in a voice channel")
@bot.command(name="pause")
async def pause(ctx):
voice = find_voice(ctx)
if not voice:
return
if voice.is_playing():
voice.pause()
else:
await ctx.send("Nothing is being played right now")
@bot.command(name="resume")
async def resume(ctx):
voice = find_voice(ctx)
if not voice:
return
if voice.is_playing():
await ctx.send("It's already playing")
else:
voice.resume()
@bot.command(name="skip")
async def skip(ctx):
id = ctx.message.guild.id
queues.set_loop(id, False)
voice = find_voice(ctx)
if not voice:
return
voice.stop()
@bot.command(name="yt")
async def youtube(ctx, url):
id = ctx.message.guild.id
result_name = manager.youtube_query(url)
queues.add(id, result_name)
if queues.get_len(id) == 1:
await start_playing(ctx, result_name)
@bot.command(name="tts")
async def text_to_speech(ctx, text):
id = ctx.message.guild.id
result_name = manager.tts_query(text)
queues.add(id, result_name)
if queues.get_len(id) == 1:
await start_playing(ctx, result_name)
@bot.command(name="clear")
async def clear_queue(ctx):
id = ctx.message.guild.id
queues.clear(id)
@bot.command(name="loop")
async def loop(ctx):
id = ctx.message.guild.id
queues.set_loop(id, True)
@bot.command(name="unloop")
async def unloop(ctx):
id = ctx.message.guild.id
queues.set_loop(id, False)
bot.run(DISCORD_TOKEN)