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

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

本文介绍了让不和谐的机器人提及其他用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

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

I am working on a bot to do some simple commands for my discord server and I haven't been able to figure out how to get the bot to mention people who aren't the author.

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

当我尝试运行命令时出现错误消息:

When I try to run the command i come up with the error message:

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'

如果我在命令之前,之后和根本没有提及的情况下使用此命令,则会发生这种情况。如果可以提供更多的信息,这里是我正在使用的导入

This happens if I use the command with a mention before, after, and not at all. If it gives some more context here are the imports I am using

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


推荐答案

您收到的特定错误是由于不等待协程引起的。 client.get_user_info 是一个协程,必须使用 await

The specific error you are getting is caused by not awaiting a coroutine. client.get_user_info is a coroutine and must use await.

如果要通过用户名提及来 + prank工作,可以使用 server.get_member_named 查找成员对象。

If you want "+prank" to work by mentioning by username, you can find a member object by using server.get_member_named.

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

Example code provided below. This will check the server the command was called from for the specified username and return the member object.

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

这篇关于让不和谐的机器人提及其他用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:22