-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
246 lines (205 loc) · 8.01 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
import discord
from discord.ext import commands, tasks
import os
from dotenv import load_dotenv
import aiohttp
import asyncio
from pytube import YouTube
from discord import VoiceState
import longs_texts
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
YOUTUBE_API_KEY = os.getenv('YOUTUBE_API_KEY')
intents = discord.Intents.default()
intents.message_content = True
intents.voice_states = True
bot = commands.Bot(command_prefix='!', intents=intents)
bot.remove_command('help')
queue = []
@bot.event
async def on_ready():
print(f'Bot {bot.user.name} logged')
@bot.command()
async def help(ctx):
embed = discord.Embed(
title = 'Bot commands',
description = 'Welcome to the help section. Here are all the commands for your fun!',
color = discord.Color.dark_orange()
)
embed.set_thumbnail(url='https://github.com/MatheusVict/Cantina-Band/assets/103688000/d04afde8-b608-490a-a30f-7da2903b2353')
embed.add_field(
name = '!help',
value = 'List all of the commands',
inline = False
)
embed.add_field(
name = '!play <Your song>',
value = 'Play a song with name and add to your queue',
inline = False
)
embed.add_field(
name = '!skip',
value = 'Skip to next music of queue',
inline = False
)
embed.add_field(
name = '!pause',
value = 'Pause your queue',
inline = False
)
embed.add_field(
name = '!resume',
value = 'Resume your queue',
inline = False
)
embed.add_field(
name = '!stop',
value = 'Stop bot',
inline = False
)
embed.add_field(
name = '!info',
value = 'Info about the creator',
inline = False
)
embed.add_field(
name = '!help_project',
value = 'You can see more informations about the projetc and to know how help us',
inline = False
)
await ctx.send(embed=embed)
@bot.command()
async def play(ctx, *, query):
voice_state = ctx.author.voice
if (voice_state is None or voice_state.channel is None):
await ctx.send("You must be in a voice channel to use this command.")
return
voice_client = discord.utils.get(bot.voice_clients, guild=ctx.guild)
if (voice_client and voice_client.is_connected()):
if (voice_client.channel != voice_state.channel):
await voice_client.move_to(voice_state.channel)
else:
voice_client = await voice_state.channel.connect()
# Search for videos using the YouTube Data API
search_url = f'https://www.googleapis.com/youtube/v3/search?key={YOUTUBE_API_KEY}&part=snippet&type=video&q={query}'
async with aiohttp.ClientSession() as session:
async with session.get(search_url) as response:
data = await response.json()
# Extract the video IDs and titles of the top 5 search results
results = data['items'][:5]
options = '\n'.join([f'{i+1}. {result["snippet"]["title"]}' for i, result in enumerate(results)])
await ctx.send(f'Top 5 search results:\n{options}\n\nPlease enter the number corresponding to the song you want to play.')
def check(message):
return message.author == ctx.author and message.channel == ctx.channel
try:
selection_msg = await bot.wait_for('message', check=check, timeout=60)
selection = int(selection_msg.content)
if (1 <= selection <= 5):
video_id = results[selection - 1]['id']['videoId']
print(f'id do video {video_id}')
voice_channel = ctx.author.voice.channel
voice_client = discord.utils.get(bot.voice_clients, guild=ctx.guild)
if (voice_client and voice_client.is_playing()):
# Add the selected video to the queue
queue.append((video_id, ctx))
await ctx.send('Song added to the queue.')
else:
# Play the selected video directly
await play_song(video_id, ctx)
else:
await ctx.send("Invalid selection.")
except asyncio.TimeoutError:
await ctx.send('No selection made. Command timed out.')
except ValueError:
await ctx.send('Invalid selection. Please enter a valid number.')
@bot.command()
async def skip(ctx):
voice_client = discord.utils.get(bot.voice_clients, guild=ctx.guild)
if (voice_client and voice_client.is_playing()):
voice_client.stop()
if (queue):
next_song = queue.pop(0)
await play_song(next_song[0], next_song[1])
await ctx.send('Skipped music. Playing the next song in the queue.')
else:
await ctx.send('Skipped music. There are no more songs in the queue.')
else:
await ctx.send("I'm not playing any music.")
@bot.command()
async def pause(ctx):
voice_client = discord.utils.get(bot.voice_clients, guild=ctx.guild)
if(voice_client and voice_client.is_playing()):
voice_client.pause()
await ctx.send("Music paused.")
else:
await ctx.send("I'm not playing any music.")
@bot.command()
async def resume(ctx):
voice_client = discord.utils.get(bot.voice_clients, guild=ctx.guild)
if (voice_client and voice_client.is_paused()):
voice_client.resume()
await ctx.send("Music resumed.")
else:
await ctx.send("I'm not playing any paused music.")
@bot.command()
async def stop(ctx):
voice_client = discord.utils.get(bot.voice_clients, guild=ctx.guild)
if (voice_client and voice_client.is_playing()):
voice_client.stop()
await voice_client.disconnect()
@bot.command()
async def info(ctx):
embed = discord.Embed(
title = 'Bot infos',
description = 'Thanks to use my bot. You can contact me clicking in my name',
color = discord.Color.dark_orange()
)
embed.set_author(name='Matheus', url='https://github.com/MatheusVict', icon_url='https://avatars.githubusercontent.com/u/103688000?v=4')
await ctx.send(embed=embed)
@bot.command()
async def help_project(ctx):
embed = discord.Embed(
title = 'You also can help this project!',
description = longs_texts.help_us_texts,
color = discord.Color.dark_orange(),
url = 'https://github.com/MatheusVict/Cantina-Band'
)
embed.set_image(url='https://github.com/MatheusVict/Cantina-Band/assets/103688000/d04afde8-b608-490a-a30f-7da2903b2353')
await ctx.send(embed=embed)
@bot.event
async def on_voice_state_update(member, before, after):
if after.channel is not None:
voice_client = discord.utils.get(bot.voice_clients, guild=member.guild)
if voice_client is not None:
if (voice_client.is_connected() and
voice_client.channel.id != after.channel.id):
await voice_client.move_to(after.channel)
async def play_song(video_id, ctx):
try:
voice_channel = ctx.author.voice.channel
voice_client = discord.utils.get(bot.voice_clients, guild=ctx.guild)
if (voice_client):
await voice_client.disconnect()
voice_client = await voice_channel.connect()
# Use pytube to get the stream URL
yt = YouTube(f'https://www.youtube.com/watch?v={video_id}')
stream = yt.streams.filter(only_audio=True).first()
url = stream.url
def play_next_song(error=None):
if (error):
print(f'Error playing song: {error}')
if (queue):
next_song = queue.pop(0)
asyncio.run_coroutine_threadsafe(play_song(next_song[0], next_song[1]), bot.loop)
ffmpeg_options = {
'options': '-vn',
"before_options": "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5"
}
voice_client.play(discord.FFmpegPCMAudio(url, options=ffmpeg_options), after=play_next_song)
await ctx.send(f'Now playing: {info["title"]}')
except Exception as e:
print(f'Error playing song: {e}')
if (queue):
next_song = queue.pop(0)
await play_song(next_song[0], next_song[1])
bot.run(TOKEN)