首页 > 解决方案 > Discord.py - 当有人提到他时,机器人不回答

问题描述

当有人在频道中提到机器人时,我尝试这样做,机器人会向用户发送一条随机消息,但我尝试过的所有事情都没有奏效。(discord.py)

channel = 797224597443051611

@bot.event
async def on_message(msg):
    chat = bot.get_channel(797224597443051611)
    if msg.channel.id != channel:
        return
    if bot.user.mentioned_in(msg):
        await chat.send(msg.author.mention + " " + random.choice(text))
    await bot.process_commands(msg)

我也尝试过这种方法,但它也不起作用。

channel = 797224597443051611

@bot.event
async def on_message(msg):
    if msg.channel.id != channel:
        return
    if bot.user.mentioned_in(msg):
        await msg.send(msg.author.mention + " " + random.choice(text))
    await bot.process_commands(msg)

标签: pythonpycharmdiscord.py

解决方案


这是我对你的问题的回答。我已经包含更多代码以获得更多上下文。

import discord
from discord.ext import commands

client = discord.Client()
client = commands.Bot(command_prefix='!')


@client.event
async def on_message(message):
   if message.author == client.user:
       return
   mention = f'<@!{client.user.id}>'
   if str(message.content).startswith(mention):
       await message.channel.send('Your message... Testing')

这应该是您正在寻找的。如果您想要随机响应,只需将发送消息更改为列表中的随机项目或文本文档中的随机行。

如果需要,我将编辑答案以包含列表中的随机响应。


推荐阅读