-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
91 lines (73 loc) · 2.83 KB
/
bot.py
File metadata and controls
91 lines (73 loc) · 2.83 KB
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
import os
import discord
import asyncio
from discord import FFmpegPCMAudio
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv("TOKEN")
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="b!", intents=intents)
queue = asyncio.Queue()
playing_task = None
@bot.event
async def on_ready():
print(f"Bot conectado como {bot.user}")
default_channel = discord.utils.get(bot.get_all_channels(), type=discord.ChannelType.text)
if default_channel:
await default_channel.send("Preparaos, listos... y fuera! Usa b!play y una url que termine en .mp3 para reproducirlo!!")
@bot.command()
async def play(ctx, *, input_text: str):
global playing_task
if not ctx.author.voice:
await ctx.send("🤡 🤡 Debeis estar en un canal de voz 🤡 🤡")
return
if ".mp3" not in input_text:
await ctx.send("No seais payaso, sabeis que debeis ingresar una URL que termine en .mp3")
return
try:
voice_client = ctx.voice_client
if not voice_client:
voice_client = await ctx.author.voice.channel.connect()
elif voice_client.channel != ctx.author.voice.channel:
await voice_client.move_to(ctx.author.voice.channel)
await queue.put((voice_client, input_text))
await ctx.send(f"🎶 Agregado a la cola: {input_text}")
if playing_task is None or playing_task.done():
playing_task = bot.loop.create_task(player_loop())
except Exception as e:
print(f"Se produjo un error: {e}")
async def player_loop():
while not queue.empty():
try:
voice_client, track = await queue.get()
voice_client.play(FFmpegPCMAudio(track))
while voice_client.is_playing():
await asyncio.sleep(1)
await asyncio.sleep(1)
except Exception as e:
print(f"Error en player_loop: {e}")
await asyncio.sleep(60)
if queue.empty():
await voice_client.disconnect()
await ctx.send("Hasta la proxima")
@bot.command()
async def skip(ctx):
if ctx.voice_client and ctx.voice_client.is_connected():
if ctx.voice_client.is_playing():
ctx.voice_client.stop()
await ctx.send("⏭️ Siguiente!")
else:
await ctx.send("Se acabo...")
else:
await ctx.send("🤡 🤡 Debeis estar en un canal de voz 🤡 🤡")
@bot.command()
async def stop(ctx):
if ctx.voice_client and ctx.voice_client.is_connected():
if ctx.voice_client.is_playing():
ctx.voice_client.stop()
await ctx.send("⏹️ Detenida.")
else:
await ctx.send("❌ No estoy conectado a ningún canal de voz.")
bot.run(TOKEN)