Change Discord Bot (Python) game or activity - bots

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'))

Related

How do I make a on_mentioned event in Nextcord python?

I want to make my Discord bot tell a member not to ping the owner.
I'm not too sure if on_mentioned is an actual event. If anybody can help,
that'd be a huge help.
Here's what I'm working with at the moment:
#bot.event
async def on_mentioned(ctx):
author = ctx.author.mention
await ctx.send(f'{author} please do not ping that member. They are either AFK or do not want to be pinged.')
You can check for mentions in on_message.
#bot.event
async def on_message(message):
for mention in message.mentions:
# do something
message.mentions will be a list of users mentioned, you can check if it contains any mentions or not.
Note about overriding on_message -- the bot will not process commands if you override this without running bot.process_commands(). You can alternatively, use a listener. See https://docs.nextcord.dev/en/latest/faq.html#why-does-on-message-make-my-commands-stop-working.
I'm not too sure if on_mentioned is an actual event
That's what docs are for. You can check the Event Reference or search for it.

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

Discord bot timed mute channel on event

So I'm new using python I'm not that good at all and I wanted to create a bot meme based when a user says "Za warudo!" the channel will get muted for 10 seconds and send an image of a gif also set
so I could do the image it was pretty easy to do I also want to add a cooldown of around 120 seconds or more I don't want this command to get spammed.
#client.event
async def on_message(message):
channel = message.channel
if message.content.startswith('ZA WARUDO!'):
with open('zawarudo.gif', 'rb') as picture:
await client.send_file(channel, picture)
anyway, I just can't understand how to do this timed channel mute should I use a role for it? if I do how am I gonna give it to everyone in the server?
i was able to do it myself
await channel.set_permissions(everyone, send_messages=False)
await asyncio.sleep(10)
await channel.set_permissions(everyone, send_messages=True)

Extract Discord user ID from a discord message

I am working on a python bot, and one thing I need is for the bot to see if someone #'d another person (i.e. I type something like "!callout #randomuser") in a discord channel. If it detects this, I want to get the user ID of the #'d user (in this case, the user ID of #randomuser)
I know this is probably a dumb question, but was unable to find an answer elsewhere. Thanks!
The easier way is to use the converters that come with the discord.ext.commands extension. That can give you a User object, which has a User.id attribute:
from discord.ext.commands import Bot
from discord import User
bot = Bot(command_prefix='!')
#bot.command(pass_context=True)
async def getid(ctx, user: User):
await bot.say("Their ID is {}".format(user.id))
#bot.command(pass_context=True)
async def callout(ctx, user: User):
await bot.say("{}, someone is talking about you".format(user.mention))
bot.run("token")
The simplest way would be using mentions. This generates a list of users, for all users that were mentioned (#user) in a message. The advantage of this is that it easily handles multiple mentions. This works in the rewrite and async version of discord.py
#commands.command()
async def test(ctx):
users = ctx.message.mentions
await ctx.send(users[0].id)

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