No audio from discord bot. discord.py/ffmpeg - python

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)

Related

discord bot bot not defined

import discord
from discord.ext import commands
import requests
import os
TOKEN = os.getenv("TOKEN")
#bot.event
async def on_message(message):
if message.content == "hello":
await message.channel.send("world")
bot.run(TOKEN)
when I start the bot in python I get the error that the bot is not defined
#bot.event
NameError: name 'bot' is not defined
you need to construct a bot first so like this
import discord
from discord.ext import commands
import requests
import os
bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
TOKEN = os.getenv("TOKEN")
#bot.event
async def on_message(message):
if message.content == "hello":
await message.channel.send("world")
bot.run(TOKEN)
You haven't made the bot yet. You are running bot.run() even though you don't have bot declared as a variable. To fix this make a discord.ext bot or a discord client.
import discord
from discord.ext import commands
import requests
import os
TOKEN = os.getenv("TOKEN")
bot = commands.Bot(command_prefix="your prefix here", intents=discord.Intents.default())
# If you want more intents, do discord.Intents.all()
# or call them manually with intents.message_content = True
#bot.event
async def on_message(message):
if message.content == "hello":
await message.channel.send("world")
bot.run(TOKEN)

Discord bot not responding for commands

I try to make a discord bot on python but he not responding for any command.
How i can fix it?
import requests
import random
from discord.ext import commands
intents = discord.Intents.all()
Away = commands.Bot(command_prefix="$", intents=intents)
#Away.event
async def on_message(ctx):
print(ctx.content)
#Away.command
async def say(ctx):
await ctx.send(ctx.content)
Away.run("token")```

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.

How do I loop a background task in discord.py and why does my code not work?

How can I send a message every x time with a discord bot with discord.py?
Here is my code:
import discord
from discord.ext import commands
from discord.ext import tasks
import os
TOKEN = 'xxxxxxxxxxx'
bot = commands.Bot(command_prefix='!',help_command=None,activity=discord.Game(name="!help"))
#tasks.loop(seconds=5)
async def ping(channel):
print("is the function being called")
channel = bot.get_channel(877115461014282320)
await channel.send('embed=embed')
bot.run(TOKEN)
Edit: I know I shouldn't leak my key but I edited it after 10 seconds and removed a letter why could someone still access it?
You have to start task using ping.start() in event on_ready or in some command.
import discord
from discord.ext import commands
from discord.ext import tasks
import os
TOKEN = os.getenv('DISCORD_TOKEN')
bot = commands.Bot(command_prefix='!', help_command=None, activity=discord.Game(name="!help"))
#tasks.loop(seconds=5)
async def ping():
print("is the function being called")
#channel = bot.get_channel(877115461014282320)
#await channel.send('embed=embed')
#bot.event
async def on_ready():
print('on ready')
ping.start()
bot.run(TOKEN)
You have also ping.stop(), ping.cancel(), etc. See doc: tasks

Discord bot that returns guild owner's username discord.py

Hello guys im trying to write a code that gives me the discord server owner but its giving Me 'None'
import discord
client = discord.Client()
TOKEN = 'token'
#client.event
async def on_message(message):
if message.content.find("getowner") != -1:
await message.channel.send(str(message.guild.owner))
client.run(TOKEN)
Can someone please help me with this bot thanks!!
I want to get the discord servers owner by typing getowner in a text channel.
I recommend using it as a command rather than doing on_message, you can do this:
from discord.ext import commands
token = "Token"
client = commands.Bot(command_prefix="!") #Use any prefix
#client.command(pass_context=True)
async def getOwner(ctx):
#await ctx.channel.send(str(ctx.guild.owner.display_name))
await ctx.channel.send(str(ctx.guild.owner))
client.run(TOKEN)
But if you don't want to use a command, you could use regular expression and just keep it as:
import discord
import re
client = discord.Client()
TOKEN = 'token'
#client.event
async def on_message(ctx):
if re.match("getowner", ctx.content):
await ctx.channel.send(str(ctx.guild.owner))
client.run(TOKEN)
EDIT: Also try using intents with your bot code.
import discord
import re
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
TOKEN = "token"
#client.event
async def on_message(ctx):
if re.match("getowner", ctx.content):
await ctx.channel.send(str(ctx.guild.owner))
client.run(TOKEN)
Same changes can be done with the command version.
It's very simple;
from discord.ext import commands
#client.command(name="getowner")
async def getowner(ctx):
await ctx.send(str(ctx.guild.owner))

Categories