Convert audio files to mp3 using ffmpeg in python - python

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.

Related

why is the FFmpegPCMAudio not playing?

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.

Discord.py bot doesn't play wav/mp3 file

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?

How to I make a my discord bot join a voice channel *in a category*?

I'd like to write a bot with the ability to join; This is my code
import json
import discord
from discord.ext import commands
JSON_FILE = r"C:\JSON PATH"
bot = commands.Bot(command_prefix = "~")
with open(JSON_FILE, "r") as json_file:
TOKEN = json.load(json_file)["token"]
GUILD = json.load(json_file)["guild"]
bot = commands.Bot(command_prefix = "~")
#bot.event
async def on_ready():
print(f"{bot.user.name} launched and has arrived for further use")
#bot.command(name = "join")
async def join(ctx, voice_channel: commands.VoiceChannelConverter):
await voice_channel.connect()
await ctx.send(f"I have joined: {voice_channel}")
#bot.command(name = "leave")
async def leave(ctx):
server = ctx.message.server
voice_client = bot.voice_client_int(server)
await voice_client.disconnect()
bot.run(TOKEN)
It can join usual channels but it cannot join channels in categories. Any advice on how to do that?
Thank you in advance
The problem is probably, that you are using an outdated version of discord.py since you are using server, which is deprecated since v1.0, and not guild. If you update it, it will probably work, because it worked for me too when I tried it.
To code it to automatically join the voice channel of the user executing the command, just use ctx.author.voice.channel:
#bot.command(name = "join")
async def join(ctx, voice_channel: commands.VoiceChannelConverter):
if not ctx.author.voice is None:
await ctx.author.voice.channel.connect()
await ctx.send(f"I have joined your voice channel.")
else:
await ctx.send("You are not in a voice channel!")
References:
discord.Member.voice
Server is deprecated

Programming a Discord bot with Python- How do I make it join a voice channel?

I want to make the bot join the voice channel that I'm in when prompted. Here's what I have:
#client.event
async def on_message(message):
if message.content.startswith('.join'):
channel = 775902254951301125
await channel.connect()
It doesn't seem to work, any tips?
I think this is what you're looking for!
#client.command(pass_context=True)
async def join(ctx):
channel = ctx.message.author.voice.voice_channel
await client.join_voice_channel(channel)
That was pulled straight from this video which was just a google search away.
try run in command prompt/powershell
py -m pip install -U discord.py[voice]
and then try again
Similar case:
Making a bot join a vc and play music
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '>')
#client.event
async def on_ready():
print ("Log : "+str(client.user))
ch = await client.fetch_channel("enter id voice channel")
await ch.connect()
client.run('token')

Discord.py error : object NoneType can't be used in "await" expression when trying to make the bot leave a voice channel

I'm currently trying to code a discord bot in python and am trying to make it join a channel, play a sound and then leave.
I came up with this code :
#bot.command()
async def sound(ctx):
channel = ctx.author.voice.channel
await join(ctx)
voice = get(bot.voice_clients, guild=ctx.guild)
source = FFmpegPCMAudio('sound.mp3')
await voice.play(source)
await channel.disconnect()
However, when I try it, after playing the sound, it doesn't disconnect and there is in my shell an error :
discord.ext.commands.errors.CommandInvokeError: Command raised an
exception: TypeError: object NoneType can't be used in 'await'
expression
I saw on forums that it could be a asyncio problem but I don't know how to resolve it.
Could someone help me please ?
Edit : my imports on my program :
import discord
import asyncio
from discord.ext import commands
from discord.utils import get
from discord import FFmpegPCMAudio
Edit v2 : the join() command :
#bot.command(pass_context=True)
async def join(ctx):
channel = ctx.author.voice.channel
voice = get(bot.voice_clients, guild=ctx.guild)
if not channel:
await ctx.send("You are not in a vocal channel.")
return
if voice and voice.is_connected():
await voice.move_to(channel)
elif voice == channel:
return
else:
await channel.connect()
You can have your join command return the VoiceClient object and use it directly to play the sound instead of using discord.utils.get. You can also get the VoiceClient object directly from the bot.voice_clients list instead of using get when checking if the bot is connected to a voice channel already.
Note that voice.play does not need await as it is not a coroutine. You also need to check if the bot stops playing audio using while voice.is_playing() before disconnecting, otherwise it will disconnect immediately after starting to play the sound.
from discord.ext import commands
from discord import FFmpegPCMAudio
bot = commands.Bot(command_prefix='?')
#bot.command()
async def join(ctx):
channel = ctx.author.voice.channel
voice = None
for vc in bot.voice_clients:
if vc.guild == ctx.guild:
voice = vc
if not channel:
await ctx.send("You are not in a vocal channel.")
return
if voice and voice.is_connected():
vc = await voice.move_to(channel)
elif voice == channel:
return
else:
vc = await channel.connect()
return vc
#bot.command()
async def sound(ctx):
voice = await join(ctx)
source = FFmpegPCMAudio('sound.m4a')
voice.play(source)
while voice.is_playing():
continue
await voice.disconnect()
bot.run('token')
The
while voice.is_playing():
continue
await voice.disconnect()
will mess up the audio.
Do this instead:
while voice.is_playing():
time.sleep(1)

Categories