Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 让不和谐机器人提及其他用户_Python_Python 3.x_Discord.py - Fatal编程技术网

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

Python 让不和谐机器人提及其他用户,python,python-3.x,discord.py,Python,Python 3.x,Discord.py,我正在开发一个机器人,为我的discord服务器执行一些简单的命令,但我还没有弄清楚如何让机器人提及那些不是作者的人 if message.content.startswith("+prank"): user = client.get_user_info(id) await client.send_message(message.channel, user.mention + 'mention') 当我尝试运行该命令时,会出现错误消息: Ignoring exc

我正在开发一个机器人,为我的discord服务器执行一些简单的命令,但我还没有弄清楚如何让机器人提及那些不是作者的人

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

您得到的特定错误是由于未等待协同路由而导致的<代码>客户端。获取用户信息是一个协同程序,必须使用
wait

如果您希望通过用户名提及“+恶作剧”起作用,您可以使用
服务器找到一个成员对象。获取名为
的成员

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

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

看起来您试图在没有实际使用
命令的情况下实现命令。不要将所有内容都放入
on\u消息
事件中。如果您不确定如何使用
discord.ext.commands
模块,可以查看


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

您从哪里获得
id
?我相信这是discord导入的一部分。如果它是
discord
导入的一部分,那么它应该看起来像
discord.id
来自discord导入id
。您是否在代码中的任何其他地方使用
id
呢?不,我不知道,我会看看这些是否能解决问题。我不相信使用
client会解决问题。get_user\u info
您需要一个字符串变量,这就是
id
应该是的。请参见此处:您计划如何使用此+恶作剧命令?
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')