-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
146 lines (113 loc) · 4.9 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
import discord
import random
import time
import os
import youtube_dl
import asyncio
from discord import app_commands
from discord.ext import commands
from discord.utils import get
from dotenv import load_dotenv
#For a more secure, we loaded the .env file and assign the token value to a variable .
load_dotenv(".env")
TOKEN = os.getenv("TOKEN")
#Intents are permissions for the bot that are enabled based on the features necessary to run the bot.
intents=discord.Intents.all()
#Command prefix is setup here, this is what you have to type to issue a command to the bot.
prefix = '!'
client = commands.Bot(command_prefix=prefix, intents=intents)
#Removed the help command to create a custom help guide
client.remove_command('help')
#--------------------------------------------------------Music Bot--------------------------------------------------------------#
voice_clients = {}
yt_dl_opts = {'format': 'bestaudio/best'}
ytdl = youtube_dl.YoutubeDL(yt_dl_opts)
ffmpeg_options = {'options': "-vn"}
#--------------------------------------------------------Events--------------------------------------------------------------#
#here we define the discord bot status and a print message on the terminal when it's on.
@client.event
async def on_ready():
await client.change_presence(activity=discord.Game(name="!help"))
print(f"Conectado como: {client.user}")
#--------------------------------------------------------Music Bot Events--------------------------------------------------------------#
@client.event
async def on_message(msg):
if msg.content.startswith("!play"):
try:
voice_client = await msg.author.voice.channel.connect()
voice_clients[voice_client.guild.id] = voice_client
except:
print("error")
try:
url = msg.content.split()[1]
loop = asyncio.get_event_loop()
data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=False))
song = data['url']
song_name = data.get('title', None)
player = discord.FFmpegPCMAudio(song, **ffmpeg_options)
voice_clients[msg.guild.id].play(player)
embed = discord.Embed(
title='Now playing',
description=f'{song_name}',
color=discord.Colour.from_rgb(32, 34, 37)
)
await msg.channel.send(embed=embed)
except Exception as err:
print(err)
if msg.content.startswith("!pause"):
embed = discord.Embed(
#title='Canción pausada',
description='Paused the song.',
color=discord.Colour.from_rgb(32, 34, 37)
)
await msg.channel.send(embed=embed)
try:
voice_clients[msg.guild.id].pause()
except Exception as err:
print(err)
if msg.content.startswith("!resume"):
embed = discord.Embed(
#title='Canción renaudada',
description='Resumed the song.',
color=discord.Colour.from_rgb(32, 34, 37)
)
await msg.channel.send(embed=embed)
try:
voice_clients[msg.guild.id].resume()
except Exception as err:
print(err)
if msg.content == ("!stop"):
await msg.add_reaction("👌")
try:
voice_clients[msg.guild.id].stop()
except Exception as err:
print(err)
if msg.content == ("!disconnect"):
await msg.add_reaction("👌")
try:
await voice_clients[msg.guild.id].disconnect()
voice_clients[msg.guild.id] = None
except Exception as err:
print(err)
#This help embed will show the commands available on the bot just by using "!help"
if msg.content == ("!help"):
embed = discord.Embed(
#title=
#description=
colour=discord.Colour.from_rgb(32, 34, 37)
)
embed.set_author(name='Help Commands', icon_url='https://media.istockphoto.com/id/1012390100/pt/vetorial/vector-logo-letter-j-wing.jpg?s=170667a&w=0&k=20&c=vLF_KnEp86Py-0x9W3SmsHPeeLAiqGQrBKQq1K7VmDU=')
embed.add_field(name="Everyone commands", value="`help`, `play`, `pause`, `ping`, `resume`, `stop`, `disconnect`", inline=False)
embed.set_footer(text="Type '.help <CommandName>' for details on a command")
#embed.add_field(name="", value="", inline=False)
await msg.channel.send(embed=embed)
#Embed that specifies the bot ping
if msg.content == ("!ping"):
embed = discord.Embed(
colour=discord.Colour.from_rgb(32, 34, 37)
)
ping = round(client.latency * 1000)
embed.add_field(name="", value="🟢 `" + str(ping) + "ms`", inline=False)
await msg.channel.send(embed=embed)
#token
client.run(TOKEN)