繁体   English   中英

让不和谐的机器人提及其他用户

[英]Getting a discord bot to mention other users

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

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

您收到的特定错误是由不等待协程引起的。 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')

看来您是在尝试执行命令而不实际使用commands 不要将所有内容都放在on_message事件中。 如果不确定如何使用discord.ext.commands模块,可以查看实验性重写分支文档

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM