Discord.py-在ping bot消息时如何发出bot消息?

时间:2020-10-12 12:12:31

标签: bots discord.py

我想当我们对机器人进行ping操作时会发送一条消息“是吗?”或类似的表情符号,但我不知道如何。那么有人可以告诉我命令pls吗?

Discord.py

3 个答案:

答案 0 :(得分:0)

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

请检查文档

答案 1 :(得分:0)

如果您不使用齿轮,请执行此操作。

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

docs:mentioned_in

答案 2 :(得分:0)

如前所述,可以使用on_message来查看机器人是否被提及。但是,除非您在末尾添加await bot.process_commands(message),否则其他命令将停止工作。这样可以确保漫游器在使用on_message处理程序之后继续侦听命令。

否则,如果您希望提及内容成为漫游器的前缀,则可以在创建客户端时将其添加到前缀函数中:

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