-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
222 lines (194 loc) · 7.21 KB
/
bot.py
File metadata and controls
222 lines (194 loc) · 7.21 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
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
import os
import glob
import discord
from os import path
from os import listdir
from dotenv import load_dotenv
from os.path import isfile, join
from pymongo import MongoClient
from discord.ext import commands
from discord_slash import SlashCommand, SlashContext
from discord_slash.utils.manage_commands import create_option
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
BOT_PREFIX = os.getenv('BOT_PREFIX')
SOUNDS_PATH = os.getenv('SOUNDS_PATH')
MONGO_URL = os.getenv('MONGO_URL')
db = MongoClient(MONGO_URL)
intros = db.bot.intros
bot = commands.Bot(command_prefix=f'{BOT_PREFIX}', intents=discord.Intents.all())
bot.volume = 0.2
slash = SlashCommand(bot, sync_commands=True)
guild_ids = [134687470733230080]
queue = []
def check_queue(voice_client):
if queue != []:
sound_effect = queue.pop(0)
voice_client.play(discord.FFmpegPCMAudio(sound_effect), after=lambda x: check_queue(voice_client))
voice_client.source = discord.PCMVolumeTransformer(voice_client.source)
voice_client.source.volume = bot.volume
@slash.slash(
name="join",
options=[],
description="Unirse a un canal de voz")
@bot.command()
async def join_channel(ctx: SlashContext):
await ctx.send(hidden=True, content="✅")
try:
channel = ctx.author.voice.channel
await channel.connect()
except Exception as e:
print(e)
print('Error al conectarse al canal de voz')
@slash.slash(
name="leave",
options=[],
description="Irse de un canal de voz")
@bot.command()
async def leave(ctx: SlashContext):
await ctx.send(hidden=True, content="✅")
try:
voice_client = discord.utils.get(ctx.bot.voice_clients, guild=ctx.guild)
await voice_client.disconnect()
except Exception as e:
print(e)
print('Error al desconectarse del canal de voz')
@slash.slash(
name="stop",
options=[],
description="Finalizar la reproducción")
@bot.command()
async def stop(ctx: SlashContext):
await ctx.send(hidden=True, content="✅")
if ctx.author.voice:
vc = discord.utils.get(ctx.bot.voice_clients, guild=ctx.guild)
if vc.is_playing():
queue = []
vc.stop()
@slash.slash(
name="s",
description="Reproducir efecto de sonido",
options=[
create_option(
name="sound",
description="Nombre del sonido",
option_type=3,
required=True
)
])
@bot.command()
async def sound(ctx: SlashContext, sound: str):
await ctx.send(hidden=True, content="✅")
sound_effect = list(glob.glob(f'{SOUNDS_PATH}/{sound}*.mp3'))[0]
print(f'playing {sound_effect}')
try:
if path.exists(sound_effect):
voice_client = discord.utils.get(ctx.bot.voice_clients, guild=ctx.guild)
if not voice_client:
channel = ctx.author.voice.channel
await channel.connect()
if ctx.author.voice:
vc = discord.utils.get(ctx.bot.voice_clients, guild=ctx.guild)
if not vc.is_playing():
print('Empty queue, playing...')
vc.play(discord.FFmpegPCMAudio(sound_effect), after=lambda x: check_queue(vc))
vc.source = discord.PCMVolumeTransformer(vc.source)
vc.source.volume = bot.volume
else:
print(f'Added to queue: {sound_effect}')
queue.append(sound_effect)
else:
await ctx.send('No estás conectado a un canal de audio')
else:
await ctx.send('No tengo ese sonido, peticiones en https://github.com/ZFP-Gaming/huevito-rey/issues')
except Exception as e:
print(e)
@slash.slash(
name="list",
options=[],
description="Listar sonidos disponibles")
@bot.command(name='sonidos', aliases=['l'])
async def sound_list(ctx: SlashContext):
files_path = f'{SOUNDS_PATH}/'
files_directory = os.listdir(files_path)
files = sorted(files_directory)
page_size = 20
pages = [files[i: i + page_size] for i in range(0, len(files), page_size)]
paginated_content = []
for page in pages:
embed = discord.Embed()
sounds_list = '```\n'
for file in page:
sounds_list += f'- {file.split(".")[0]}\n'
sounds_list += '```'
embed.add_field(name='🔈 Lista de sonidos disponibles', value=sounds_list, inline=False)
paginated_content.append(embed)
buttons = [u"\u23EA", u"\u2B05", u"\u27A1", u"\u23E9"]
current = 0
msg = await ctx.send(embed=paginated_content[current])
for button in buttons:
await msg.add_reaction(button)
while True:
try:
reaction, user = await bot.wait_for("reaction_add", check=lambda reaction, user: user == ctx.author and reaction.emoji in buttons, timeout=60.0)
except Exception as e:
return print(e)
else:
previous_page = current
if reaction.emoji == u"\u23EA":
current = 0
elif reaction.emoji == u"\u2B05":
if current > 0:
current -= 1
elif reaction.emoji == u"\u27A1":
if current < len(paginated_content)-1:
current += 1
elif reaction.emoji == u"\u23E9":
current = len(paginated_content)-1
for button in buttons:
await msg.remove_reaction(button, ctx.author)
if current != previous_page:
await msg.edit(embed=paginated_content[current])
@slash.slash(
name="volume",
description="Cambiar el volumen",
options=[
create_option(
name="amount",
description="Número entre 1 y 100",
option_type=4,
required=True
)
])
@bot.command()
async def volume(ctx: SlashContext, amount: int):
try:
bot.volume = int(amount)/100
await ctx.send(f'El volumen actual es {amount}%')
except Exception as e:
print(e)
await ctx.send('')
@bot.event
async def on_voice_state_update(member, before, after):
try:
voice_client = discord.utils.get(bot.voice_clients, guild=member.guild)
if before.channel is None and after.channel is not None and member.bot == False:
if voice_client and voice_client.channel == after.channel:
id = member.id
data = intros.find_one({'id': id})
sound_effect = f'{SOUNDS_PATH}/{data["effect"]}.mp3'
if data and data['effect'] != '' and path.exists(sound_effect):
voice_client.play(discord.FFmpegPCMAudio(sound_effect))
voice_client.source = discord.PCMVolumeTransformer(voice_client.source)
voice_client.source.volume = bot.volume
else:
print(f'{member.name} no tiene un sonido registrado')
if voice_client and voice_client.channel == before.channel:
connected_users = voice_client.channel.members
if len(connected_users) == 1 and connected_users[0].bot:
print('Voice channel empty, leaving...')
await voice_client.disconnect()
except Exception as e:
print(e)
print('START')
bot.run(TOKEN)