Glitter
Glitter

Reputation: 49

Discord.py- How to make a bot message when its pinged?

I want like when we ping the bot it sends a message 'Yes?' or something like that with an emoji but i can't figure out how. So can anyone tell me the command pls?

Discord.py

Upvotes: 0

Views: 3860

Answers (3)

overman
overman

Reputation: 13

As previously mentioned, can use on_message to see if the bot is mentioned. However, the other commands will stop working unless you add await bot.process_commands(message) at the end. This will ensure that the bot continues to listen to commands after the on_message handler is used.

Otherwise, if you want a mention to function as the bot's prefix, you can add this to the prefix function when creating the client:

import discord

from discord.ext import commands

default_prefixes = ['!', '?', '.']

def get_prefix(bot, message):
    return commands.when_mentioned_or(*default_prefixes)(bot, message)

bot = commands.Bot(command_prefix=get_prefix, intents=discord.Intents.all())

Upvotes: 1

interesting
interesting

Reputation: 11

if you're not using cogs then do this.

@client.event
async def on_message(message):
    if client.user.mentioned_in(message):
        await message.channel.send("Hmmm?")

docs: mentioned_in

Upvotes: 1

Phani Rithvij
Phani Rithvij

Reputation: 4487

import discord

class MyClient(discord.Client):
    async def on_ready(self):
        print('Logged on as {0}!'.format(self.user))

    async def on_message(self, message: discord.Message):
        print('Message from {0.author}: {0.content}'.format(message))
        mentions = [str(m) for m in message.mentions]
        if str(self.user) in list(mentions):
            text = self.text_message(message)
            if text == "":
                print("Bot was mentioned")
            else:
                print("Bot was mentioned with message", text)


    def text_message(self, message: discord.Message) -> str:
        raw = message.raw_mentions
        content: str = message.content
        for m in raw:
            content = content.replace(f'<@!{m}>', '')
        return content

client = MyClient()
client.run("TOKEN")

Check the docs please

Upvotes: 0

Related Questions