I am coding my first music bot and implemented joining voice chat, but there was a problem when playing music from youtube. I'm trying to play music from the received YouTube link and getting an error. I tried to find a solution on the Internet, but everywhere the code is from the old documentation. Here is my code:
#client.command()
async def play(ctx, url):
guild = ctx.message.guild
voice_client = guild.voice_client
player = await voice_client.create_ytdl_player(url)
players[guild.id] = player
player.start()
Error in the console: AttributeError: 'VoiceClient' object has no attribute 'create_ytdl_player'
That property of voice clients has not existed ever since rewrite (1.x). I would reccomend you to take a look at the discord.py github examples, which one contains a music bot that uses youtube-dl
Related
I have a bot that currently does nothing other than keep itself hosted online
I have tried finding an answer to this but everything is for Javascript
basically what I'm looking for is a bot that:
When someone sends a message detect if he has role "X"
If he has role "X" reply with a prewritten message
for example, if someone has a role named "PlaysROR2"
whenever he types the word game the bot will send the message ROR2
I'm new to making discord bots and python in general so the more specific the answer the better
from what I understood there's no Has_role function so it's pretty complicated
#client.event
async def on_message(message):
role = discord.utils.get(message.guild.roles, name="PlaysROR2")
if role in message.author.roles and message.content == "game":
await message.channel.send("ROR2")
else:
return
Try this? I'm not very good at discord.py, sorry, and I haven't tested this yet so it might not work.
I want my bot to send a message to a specific guild channel whenever it is added or removed from any guild. How do I make it in Discord.py? I am aware of client.event but I am unsure how to use it.
My bots code (sends to channel at the top of server where it has permissions)
#client.event
async def on_guild_join(guild):
for channel in guild.text_channels:
if channel.permissions_for(guild.me).send_messages:
embedHi = discord.Embed(
title="Thanks for adding me!",
description=
f"<:impostor:774673531786625024>I am the Impostor - a bot created by Baz!<:impostor:774673531786625024>\n\n<:noice:751384305464377375>You can join my support server by running $help and you can view all of my commands here as well!<:noice:751384305464377375>\n\n<:patreon:839897502925062165> Feel free to go to https://www.patreon.com/theimpostor to gain access to cool premium commands! <:patreon:839897502925062165>\nIf you join the <:purple:839879572631453696> Hacker Plan <:purple:839879572631453696>, then you will recieve all premium commands, a special role, early access to commands and even work in progress updates!\n:partying_face:Have fun!:partying_face:\n\n\n<:ping:757276110252670986>When you added this bot, it was in version {__version__}<:ping:757276110252670986>",
url="https://www.patreon.com/theimpostor",
colour=discord.Colour.red())
embedHi.set_thumbnail(
url=
"image url"
)
embedHi.set_image(url="image url")
embedHi.set_footer(
text="© Baz - The Impostor - Among Us bot for Discord")
await channel.send(embed=embedHi)
break
#bot.event
async def on_guild_join(guild):
# send your message
Relevant docs
I have already done that google assistant sends message to my text channel and I want that my other bot listen only to my google Assistant bot. I have found a way a little... tricky, by changing the method process_command() to avoid the return when the id of the message is from my google assistant.
I don't think that it is a good solution, and it would only work on my computer and it is working right now, another problem is that the bot must be in a voice channel to work. So I've been thinking, on the function on_message, with startswith("whatever character") to the message.content I can filter the messages of my bot and go directly to the function, but I need the parameter ctx that I don't know how to pass it because on_message the only parameter that it has is "message".
How can I get the ctx from the function on_message? I'm open to new suggestion about how do it on another way.
Use the get_context method
ctx = await bot.get_context(message) # or `client.get_context`
Reference:
Bot.get_context
I'm currently programming a bot and my first goal is to make the bot welcome and say goodbye to members that join or leave, but the bot sends nothing, not even showing any error.
#client.event
async def on_member_join(member):
print(" System: A member has joined the server!")
def sprint(str):
for c in str + '\n':
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(3./90)
sprint(" Bot: Oh hey, looks like "+member.name+" has joined..")
#send message to a specific channel
channel = client.get_channel(channel id from the welcome channel)
await channel.send("Hey! Welcome to the server, "+member.mention+"! I hope you read the #rules before doing anything, otherwise have fun and enjoy your stay!")
I did it last time before I recreated the script, but the on_member_join thing doesn't work, not even on_member_leave! (I'm doing the on_member_join first to check if it's working)
My alternate account rejoined my server, but the bot (except MEE6) didn't send anything.. Is there anything you guys can help me?
You'll need to enable the member intent by creating an instance of discord.Intents.default, setting Intents.members to True, and passing the instance into your commands.Bot or discord.Client constructor, like:
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(intents=intents)
You'll also need to enable the members intent in the Discord dev portal (https://discord.com/developers). Go to that url, click on your application, go to the Bot tab, scroll down to the 'privileged gateway intents' section, and enable the members intent.
Then, restart your bot, and member events should work.
(for more info, look at https://discordpy.readthedocs.io/en/latest/intents.html)
I am trying to make it so server moderators are able to temporarily mute users in their Discord. I am not an experienced developer within Discord bots but learning.
What I am trying to do?
I am trying to make it so server owners are able to temp mute users in their Discord servers.
This is what I currently have for my Mute command:
#bot.command(pass_context=True)
async def mute(ctx, user: discord.Member):
if ctx.message.author.server_permissions.kick_members:
role = discord.utils.get(user.server.roles, name="Muted")
embed = discord.Embed(title="{} has been muted!".format(user.name), description="When the user needs unmuting do !unmute #user!" , color=0x0072ff)
embed.set_footer(text="Reversed by Damian#9209 | Reduction#9975")
embed.set_thumbnail(url=user.avatar_url)
await bot.add_roles(user, role)
await bot.say(embed=embed)
else:
embed = discord.Embed(title="Permission Denied.", description="You don't have permission to use this command.", color=0xff0000)
embed.set_footer(text="Reversed by Damian#9209 | Reduction#9975")
await bot.say(embed=embed)
As I am sure you are aware, the Discord.py library has changed to no longer include the rewrite branch. This means that the mute command has been made way easier for you. I'll provide some code to get you started. I will also provide links to all the documentation to each line.
#bot.command(name="tempmute",description="Temporarily mute a member")
#commands.has_permission(mute_members=True)
async def _tempmute(ctx,user:discord.Member):
muteRole = discord.utils.get(ctx.guild.roles,name="Muted")
await user.add_roles(muteRole)
has_permission is a check for Discord user permissions
utils.get is a utility function that can step through iterables and find the correct search filter. Here, we are using it to find a role with the name "Muted"
add_roles is a coroutine function (aka needs to be awaited) that adds one or multiple roles to a user.
I suggest also making sure that the role has the speak permission disabled so that you don't have to deal with it through the on_message event. Hope that helps!