【问题标题】:Discord.py - bot doesnt answer when someone mentions himDiscord.py - 当有人提到他时,机器人不回答
【发布时间】:2021-01-17 02:28:46
【问题描述】:

当有人在频道中提到机器人时,我正在尝试这样做,机器人会向用户发送一条随机消息,但我尝试过的所有事情都没有奏效。 (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)

【问题讨论】:

  • 有什么错误吗?
  • 不,一切正常,但机器人没有反应。
  • 第一个代码应该可以正常工作,除非text 变量没有问题。
  • 你在哪里声明channel = 797224597443051611?尝试在 on_message 函数中这样做。
  • 由于某种原因它不起作用,我知道它应该,我尝试了使用通道的方法,但它也不起作用。

标签: python pycharm discord.py


【解决方案1】:

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

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

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

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

【讨论】:

  • 我试过你的方法:@bot.event async def on_message(msg):mention = f'' if str(msg.content).startswith(提及):等待 msg.channel.send('您的消息......测试')但它仍然不起作用,我认为它必须与我使用的东西有关:bot = commands.Bot(command_prefix =“!”)所以我将其更改为: bot = discord.Client() 但仍然无法正常工作。
  • 我将用更多可能解决问题的代码重写我的答案。
  • 是的,它现在可以工作了,而且我发现即使我将它附加到另一个事件,它也可以工作,不知道为什么,但我终于可以继续前进了,谢谢你的帮助。
【解决方案2】:

你必须在消息事件中寻找它:

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

【讨论】:

    猜你喜欢
    • 2021-10-20
    • 2019-12-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2023-02-06
    • 2022-01-23
    • 2020-09-14
    相关资源
    最近更新 更多