in the following code of a discod bot (wrote in python) the FFmpegPCMAudio is not playing, the code and the mp3 audio are in the same folder, also, yesterday the bot worked well, but today when i write the command the bot enters the voice chat, but it doesn't play the mp3 audio, why?
import discord as ds
from discord.ext import commands
from discord import FFmpegPCMAudio
intents = ds.Intents.all()
bot = commands.Bot(command_prefix='$', intents = intents)
#bot.event
async def on_ready():
print('bot online.')
#bot.command(pass_context = True)
async def outro(ctx):
if (ctx.author.voice):
channel = ctx.message.author.voice.channel
voce = await channel.connect()
source = FFmpegPCMAudio('outro.mp3')
player = voce.play(source)
else:
await ctx.send('not in a voice chat')
#bot.command(pass_context = True)
async def esci(ctx):
if (ctx.voice_client):
await ctx.guild.voice_client.disconnect()
await ctx.send('adios #everyone')
else:
ctx.send('not in a voice chat')
bot.get_command('outro')
bot.get_command('esci')
bot.run('MTAwNDA0MjQ1MjcxMTI0NzkwNQ.GjUNFS.LecuRh7QuWxrdpK-lqyH3npMUzCiIyzehCQfjU')
If you're running this on Replit, it's because Replit no longer supports ffmpeg. If not, then you don't have it installed properly on your system.
Related
import discord
from discord.ext import commands
from discord import FFmpegPCMAudio
import requests
import json
from apikeys import *
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix = '!', intents=intents)
#client.event
async def on_ready():
print("The bot is now ready for use!")
print("-----------------------------")
#client.command(pass_context = True)
async def join(ctx):
if (ctx.author.voice):
channel = ctx.message.author.voice.channel
voice = await channel.connect()
source = FFmpegPCMAudio("song.wav")
player = voice.play(source)
else:
await ctx.send("You are not in a voice channel")
#client.command(pass_context = True)
async def leave(ctx):
if (ctx.voice_client):
await ctx.guild.voice_client.disconnect()
await ctx.send("I left the voice channel")
else:
await ctx.send("I am not in a voice channel")
client.run(BOTTOKEN)
Hey! I have recently started watching a tutorial series.
It works for the tutorial maker, but doesn't for me.
The bot is able to join the voice channel the user is in, but never starts playing "song.wav".
Sadly Visual Studio Code also doesn't output any Error messages.
Does anyone know a solution?
I'm working on the BOT industry for Dyscord and I need to play a voice clip in a kinetic knowledge I have a 32-bit computer
import discord
from discord.ext import commands
import os
client = commands.Bot(command_prefix="!")
#client.command()
async def play(ctx, url : str):
print('We have logged in as {0.user}'.format(client))
voiceChannel = discord.utils.get(ctx.guild.voice_channels, name="Lounge")
await voiceChannel.connect()
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
voice.play(discord.FFmpegPCMAudio("song.mp3"))
#client.command()
async def ext(ctx):
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
await voice.disconnect()
client.run('token')
Heyo! I recommend checking out this GitHub repo: https://github.com/pariweshsubedi/python-ffmpeg-audio-converter
It'll help make things easier.
I just started learning python recently and never coded a discord bot before. I've been trying to get the bot to connect to the voice or respond to ">hello", but it doesn't respond to my command and I don't know how to fix it. I'm using replit as hosting.
import discord
import os
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix = '>', intents=intents)
#client.event
async def on_ready():
print('Log in as {0.user}'.format(client))
print('-------------------')
#client.command()
async def hello(ctx):
await ctx.send("Hello Im Deemo Bot")
#client.command(pass_context = True)
async def join(ctx):
if (ctx.author.voice):
channel = ctx.message.author.voice.channel
await channel.connect()
else:
await ctx.send('You are not in a voice channel')
#client.command(pass_context = True)
async def leave(ctx):
if (ctx.voice_client):
await ctx.guild.voice_client.disconnect()
await ctx.send('Deemo left voice')
else:
await ctx.send('Deemo is not in voice')
keep_alive()
client.run(os.environ['TOKEN'])
I'm creating a simple music bot and I cant figure out why there is no audio from the bot. I've tried kicking and rejoining, changing code and more. I just cant figure this out. There is no errors from the command line and the bot has a little green circle around it as if there was audio from it. However it goes away if you rejoin the VC.
Here is the code:
import discord
from discord.utils import get
from discord.ext import commands
from discord import FFmpegPCMAudio
from discord.voice_client import VoiceClient
import youtube_dl
import os
TOKEN = "Token Here"
bot = commands.Bot(command_prefix='s')
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix=',', intents = intents)
#bot.event
async def on_ready():
channel = discord.utils.get(bot.get_all_channels(), id=794444804607574026)
await channel.connect()
#bot.command(name='play')
async def play(ctx, url):
await message.content.startswith('play')
player = await voice_client.create_ytdl_player(url)
player = await voice.create_ytdl_player("https://youtu.be/K4DyBUG242c")
player = await vc.create_ytdl_player()
player.start()
bot.run(TOKEN)
If you want to, feel free to improve it. I got this code from a YouTube tutorial so I don't completely understand the audio part.
Try this. When you run the command the message will never start with play, the bot prefix is ",".
Also, you add intents to client. You should set it to bot instead and delete client, you never use it in the code.
import discord
from discord.utils import get
from discord.ext import commands
from discord import FFmpegPCMAudio
from discord.voice_client import VoiceClient
import youtube_dl
import os
TOKEN = "Token Here"
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix=',', intents = intents)
#bot.event
async def on_ready():
channel = discord.utils.get(bot.get_all_channels(), id=794444804607574026)
await channel.connect()
#bot.command(name='play')
async def play(ctx, url):
# await message.content.startswith('play')
player = await voice_client.create_ytdl_player(url)
player = await voice.create_ytdl_player("https://youtu.be/K4DyBUG242c")
player = await vc.create_ytdl_player()
player.start()
bot.run(TOKEN)
So I started working on a music bot and I keep getting this when I try to leave. Joining a voice channel works fine.
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'disconnect'
And my code is
#client.command(name = "join",
pass_context = True)
async def join(ctx):
channel = ctx.message.author.voice.voice_channel
await client.join_voice_channel(channel)
print("Bot joined the voice channel")
#client.command(name = "leave",
pass_context = True)
async def leave(ctx):
server = ctx.message.server
voice_client = client.voice_client_in(server)
await voice_client.disconnect()
print("Bot left the voice channel")
Also im using the following libraries:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
from discord import Game
import random
If you don't have a VoiceClient in that server, then Client.voice_client_in will return None. You should check for this before attempting to disconnect.
#client.command(name = "leave",
pass_context = True)
async def leave(ctx):
server = ctx.message.server
voice_client = client.voice_client_in(server)
if voice_client:
await voice_client.disconnect()
print("Bot left the voice channel")
else:
print("Bot was not in channel")
The fixed version with the input from #PatrickHaugh is:
Imports:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
from discord import Game
from ctypes.util import find_library
import random
from discord import opus
import nacl
import asyncio
Code:
#client.command(name = "join",
pass_context = True)
async def join(ctx):
opus_path = find_library('opus')
discord.opus.load_opus(opus_path)
if not opus.is_loaded():
print('Opus was not loaded')
else:
channel = ctx.message.author.voice.voice_channel
await client.join_voice_channel(channel)
print("Bot joined the voice channel")
#client.command(name = "check",
pass_context = True)
async def check(ctx):
server = ctx.message.server
if client.is_voice_connected(server):
print("Yes")
else:
print("No")
#client.command(pass_context = True)
async def leave(ctx):
for x in client.voice_clients:
if(x.server == ctx.message.server):
return await x.disconnect()
return await client.say("I am not connected to any voice channel on this server!")