Issue
import discord
import os
import asyncio
import youtube_dl
TOKEN = "removed"
intents = discord.Intents.default()
intents.typing = False
intents.presences = False
client = discord.Client(intents=discord.Intents.default())
voice_client ={}
yt_dl_opts = {'format' : 'bestaudio/best'}
ytdl = youtube_dl.YoutubeDL(yt_dl_opts)
ffmpeg_options = {'options': "-vn"}
@client.event
async def on_ready():
print("{0.user} is online!".format(client))
@client.event
async def on_message(message):
if message.content.startswith("\play"):
try:
url = message.content.split()[1]
voice_client = await message.author.voice.channel.connect()
voice_client[voice_client.guild.id] = voice_client
loop = asyncio.get_event_loop()
data = await loop.run_in_executer(None, lambda: ytdl.extract_info(url, download=False)) #to get data from url
song = data['url']
player = discord.FFmpegPCMAudio(song, **ffmpeg_options, executable="C:\\ffmpeg\\bin\\ffmpeg.exe")
voice_client.play(player)
except Exception as err:
print(err)
client.run(TOKEN)
the bot runs and become online but never accepts or read a command on the discord though i have given all rights.Please help me out as soon as possible.This is a music bot and i have already installed all the modules needed in this code
Solution
You need to include message intents and message content intents to allow your bot to read the messages. I tested it like this and it worked.
import discord
import os
import asyncio
import youtube_dl
TOKEN = "TOKEN_HERE"
intents = discord.Intents.default()
intents.messages = True
intents.message_content = True
client = discord.Client(intents=intents, messages = True, message_content = True)
voice_client ={}
yt_dl_opts = {'format' : 'bestaudio/best'}
ytdl = youtube_dl.YoutubeDL(yt_dl_opts)
ffmpeg_options = {'options': "-vn"}
@client.event
async def on_ready():
print("{0.user} is online!".format(client))
@client.event
async def on_message(message):
if message.content.startswith("\play"):
try:
url = message.content.split()[1]
voice_client = await message.author.voice.channel.connect()
voice_client[voice_client.guild.id] = voice_client
loop = asyncio.get_event_loop()
data = await loop.run_in_executer(None, lambda: ytdl.extract_info(url, download=False)) #to get data from url
song = data['url']
player = discord.FFmpegPCMAudio(song, **ffmpeg_options, executable="C:\\ffmpeg\\bin\\ffmpeg.exe")
voice_client.play(player)
except Exception as err:
print(err)
client.run(TOKEN)
Answered By - SuperKingCheese
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.