Discord Bot isn't playing music - audio

I have been following a discord bot tutorial from 2 years ago. As you know, they reworked discord.py and after about an hour of searching I have found people with the same issue but no answer.I keep getting "AttributeError: 'VoiceClient' object has no attribute 'create_ytdl_player'". If anyone has anything, it would be appreciated.
#client.command(pass_context = True)
async def play(ctx):
guild = ctx.message.guild
voice_client = ctx.guild.voice_client
player = await voice_client.create_ytdl_player("https://youtu.be/HKe1jFjojzA")
players[guild.id] = player
player.start()

You're using syntax from a previous version of discord.py, which doesn’t work anymore.
Have a look at the changes made when discord.py migrated to v1.
EDIT:
Here's an example

Related

Bot won't leave voice channel when left alone

I have a personal music bot that plays music for me and friends, I only have a play and leave commands which both work perfectly, but I was wondering if a little upgrade to the bot automatically leaving voice channel when left in it alone was possible. After some reading in documentation, I thought I got it but this piece of my code simply doesn't work, it's almost like python is ignoring it for some reason so I guess I am missing something here...
so I was wondering if there is a reason why my code:
#client.event
async def on_voice_state_update(member):
voice_state = member.guild.voice_client
if len(voice_state.channel.members) == 1:
await voice_state.disconnect()
won't work, I don't get any error messages and literally nothing happens. Is it all as it should be here?
You are missing some arguments for this event. Simply add before and after and you should be fine.
The full event could be:
#client.event
async def on_voice_state_update(member, before, after):
voice_state = member.guild.voice_client
if voice_state is not None and len(voice_state.channel.members) == 1:
# If the bot is connected to a channel and the bot is the only one in the channel
await voice_state.disconnect() # Disconnect the bot from the channel
You can see more in the docs here:
on_voice_state_update

Playing audio files with discord.py

I've recently gotten back into coding and Python and wanted to check out how to make a discord bot. I've managed to send messages and do basic things, but now i can't figure out how to make it play audio in a voice channel.
I can get it to join and leave at will, but as soon as I try to play music, it's just silent. The audio file is already downloaded and it's in the same folder as my program. I do not get any errors, all i know is there is something wrong with the bot. The audio file is not just silence, I have checked. I do not know what is wrong.
What should happen is when i call for the bot and it joins my VC, it should start playing the song.
My code:
#client.command(aliases=['jvc'])
async def joinvc(ctx):
voice_channel = ctx.message.author.voice.channel
voice_client = discord.utils.get(client.voice_clients, guild=ctx.guild)
if voice_client:
if not voice_client.is_connected():
await voice_channel.connect()
voice_client.play(discord.FFmpegPCMAudio(executable='C:\\FFmpeg\\bin\\ffmpeg.exe', source='file.mp3'))
else:
await voice_channel.connect()
voice_client = discord.utils.get(client.voice_clients, guild=ctx.guild)
voice_client.play(discord.FFmpegPCMAudio(executable='C:\\FFmpeg\\bin\\ffmpeg.exe', source='file.mp3'))
Sorry if the code isn't great, but it was the only way I could think of without getting errors. Also I apologize if I forgot anything important or did something wrong, this is my first time using StackOverflow.
Remember to add ffmpeg to path
#client.command()
async def thisisnotacommand(ctx):
channel = ctx.author.voice.channel
vc = await channel.connect()
await ctx.send('Started playing: something')
vc.play(discord.FFmpegPCMAudio('file.mp3'), after=lambda e: print('done', e))
If you don't know how to add ffmpeg to path feel free to msg me Special unit#5323

Make discord python rewrite bot tag message author

Am updating an old bot made with Discord.py and I can't get it to tag people as it used to do.
if 'test' in message.content:
msg = f'test sucessful {.author}'
await message.channel.send(msg)
On the discord rewrite documentation the only example I found was with {.author}, and it doesnt work. It does print the user's name, like person#1234, but it doesnt tag them.
There are different ways of tagging a user - they are equivalent:
Examples
await message.channel.send(f"Test successful, {message.author.mention}!")
await message.channel.send(f"Test successful, <#{message.author.id}>!")

Change Discord Bot (Python) game or activity

I am trying to repair my Discord Bot and I cannot get any further can someone help me there?
await client.change_presence(activity=discord.Activity(name="<hilfe for help."))
The code should change the activity but it does nothing and just gives me a error.
I don't know what to change to get it working.
Here it is. It have to be discord.Game not discord.Activity
#bot.event
async def on_ready():
await bot.change_presence(activity=discord.Game('bread'))

How to get bot avatar link

It looks silly question but I want to embed bot avatar so how to do it. Like embed.set_thumbnail(url=user.avatar_url) I tried bot.avatar_url but it's not working.
Assuming you are using bot = discord.Client, Client does not have avatar_url as an attribute. Luckily though, Client does have a user attribute you can access, which means in your case you should be able to use bot.user.avatar_url.
Documentation here: http://discordpy.readthedocs.io/en/latest/api.html#discord.Client.user
Client or i should say bot, itself dont have an attribute of avatar_url cuz its just a discord client, so you need to use bot.user.avatar_url instead
#bot.command()
async def botavatar(ctx):
BotAvatar = bot.user.avatar_url
embed = discord.Embed(
title=f'{bot.user.name}\'s Avatar:',
color=discord.Colour.red())
embed.set_thumbnail(url=f'{BotAvatar}')
await ctx.send(embed=embed)
Actually, Due to recent updates to discord.py, you can get the bot's avatar url from bot.user.avatar.url
So, in a command
#bot.command()
async def avatar(ctx):
await ctx.send("{}".format(self.bot.user.avatar.url))
Hope that helps!

Resources