【问题标题】:Getting a discord bot to mention other users让不和谐的机器人提及其他用户
【发布时间】:2018-08-21 05:53:04
【问题描述】:

我正在开发一个机器人来为我的不和谐服务器执行一些简单的命令,但我无法弄清楚如何让机器人提及不是作者的人。

if message.content.startswith("+prank"):
        user = client.get_user_info(id)
        await client.send_message(message.channel, user.mention + 'mention')

当我尝试运行命令时,我想出了错误消息:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "C:/Users/user/Desktop/Murk-Bot 2.0.py", line 130, in on_message
    await client.send_message(message.channel, user.mention + 'mention')
AttributeError: 'generator' object has no attribute 'mention'

如果我在命令之前、之后和根本不提及时使用该命令,就会发生这种情况。如果它提供了更多上下文,这里是我正在使用的导入

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import random

【问题讨论】:

  • 你从哪里得到id
  • 我相信这是不和谐导入的一部分。
  • 如果它是discord 导入的一部分,那么它应该类似于discord.idfrom discord import id。您是否在代码中的其他任何地方使用id
  • 不,我不会,我会看看有没有解决问题的。
  • 我不相信它会因为使用client.get_user_info 你需要一个字符串变量,这就是id 应该是什么。见这里:discordpy.readthedocs.io/en/latest/… 打算如何使用这个 +prank 命令?

标签: python python-3.x discord.py


【解决方案1】:

您似乎在尝试执行命令而不实际使用commands。不要将所有内容都放在on_message 事件中。如果不确定如何使用discord.ext.commands 模块,可以查看documentation for the experimental rewrite branch

import discord
from discord.ext.commands import Bot

bot = Bot(command_prefix='+')

@bot.command()
async def prank(target: discord.Member):
    await bot.say(target.mention + ' mention')

bot.run('token')

您可以将此命令与+prank Johnny 一起使用。然后,机器人将在同一频道中响应 @Johnny mention

【讨论】:

    【解决方案2】:

    你得到的具体错误是由于没有等待协程引起的。 client.get_user_info 是协程,必须使用await

    如果你想让“+prank”通过用户名提及来工作,你可以使用server.get_member_named找到一个成员对象。

    下面提供的示例代码。这将检查从指定用户名调用命令的服务器并返回 member 对象。

    if message.content.startswith("+prank"):
        username = message.content[7:]
        member_object = message.server.get_member_named(username)
        await client.send_message(message.channel, member_object.mention + 'mention')
    

    【讨论】:

      猜你喜欢
      • 2020-05-18
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 2020-12-30
      • 2021-11-10
      • 2019-04-16
      • 2020-12-06
      相关资源
      最近更新 更多