How do I make a on_mentioned event in Nextcord python? - python-3.x

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.

Related

AIOGram. How to fix chat_member_handler?

I need my bot to do specific things when an user is joining end exiting a group. First, I write proof-of-concept code:
#dp.chat_member_handler()
async def user_joined_chat(update: types.ChatMemberUpdated):
print('Users changed')
But that does nothing. I added-deleted the test user to the test group many times, but nothing. Of course, I have made sure "privacy mode" is disabled and the bot is an administrator of the group before.
What's wrong? Do I use wrong handler?
You have to use other handler, handler that you are trying to use handles ChatMember status changes.
You have to use classic message_handler and handle content types like: NEW_CHAT_MEMBERS and LEFT_CHAT_MEMBER you can find such types here
so there is working code:
#dp.message_handler(content_types=[types.ContentType.NEW_CHAT_MEMBERS, types.ContentType.LEFT_CHAT_MEMBER])
async def user_joined_chat(message: types.Message):
print('Users changed')

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

Input Messages in Discord.py

I'm trying to find a way to program my bot to clear a specific amount of messages in a channel. However, I do not know how to get my bot to run it's code based on the user's input data. For example, let's say I'm a user who wants to clear a specific amount of messages, like let's say 15 messages. I want my bot to then clear 15 messages exactly, no more, no less. How do I do that?
if message.content == "{clear":
await message.channel.send("Okay")
await message.channel.send("How many messages should I clear my dear sir?")
This is legit all I got lmao. I'm sorry that I'm such a disappointment to this community ;(
Using a on_message event, you'd have to use the startswith mehtod and create a amount variable which takes your message content without {clear as a value:
if message.content.startswith("{clear"):
amount = int(message.content[7:])
await message.channel.purge(limit=amount+1)
However, I don't recommend using on_message events to create commands. You could use the commands framework of discord.py. It will be much easier for you to create commands.
A quick example:
from discord.ext import commands
bot = commands.Bot(command_prefix='{')
#bot.event
async def on_ready():
print("Bot's ready to go")
#bot.command(name="clear")
async def clear(ctx, amount: int):
await ctx.channel.purge(limit=amount+1)
bot.run("Your token")
ctx will allow you to access the message author, channel, guild, ... and will allow you to call methods such as send, ...

Discord Bot cannot send messages because send is not an attribute of context

I am re implementing my commands the correct way accordingly to the documentation, using context and command decorators instead of on_message listeners, transferring my commands over is kind of a pain but the documentation has been rather helpful thankful. Unfortunately I've run into an issue which prevents me from sending messages...
Before the move, the way I would send messages was like this
#client.event
async def on_message(message):
if message.author.id in AdminID:
await client.send_message(message.channel. 'message')
Unfortunately this does not work on the new format because there is no message argument to get information from, what you have to do is use is the ctx (context) argument instead which looks something like this according to the documentation
#bot.command()
async def test(ctx, arg):
await ctx.send(arg)
Although the bot recognizes the command and goes there, i cannot send a message because send is not an attribute of ctx, this code is taken strait out of the documentation, am I missing something? Can someone help me figure this out? Thank you
You're looking at the documentation for a different version of the library than the one you're using.
You're using version 0.16, also called the "async" branch. The documentation for that branch is here
You're reading the documentation for the 1.0 version, also called the rewrite branch.
Your command would look something like
#bot.command(pass_context=True)
async def test(ctx):
if ctx.message.author.id in AdminID:
await client.send_message(ctx.message.channel, 'message')

Discord.py how to tell if a player has sent a message, of any type

I want to make a exp system, and to do that, I need to see if a user sends any message, so, how would I check that in discord.py?
As per the documentation you can do something like this:
#client.event
async def on_message(msg):
print("{} user has sent a message in {}".format(msg.author.name,msg.channel.name))
#whatever you want to do with the XP system

Resources