Discord Bot-AttributeError:“客户端”对象没有属性“命令”

时间:2019-05-29 22:37:14

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

我正在尝试创建一个新的Discord机器人,尝试创建一条消息通知该机器人当前所在的所有Discord服务器时,都会出现问题。

我试图无济于事地解决问题,包括查找问题,阅读文档以及尝试新代码。


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

TOKEN = [REDACTED]



# client = discord.Client()

client = Bot("!")

@client.event
async def on_message(message):
    # we do not want the bot to reply to itself
    if message.author == client.user:
        return

    if message.content.startswith('!hello'):
        @client.command(pass_context=True)
        async def broadcast(ctx, *, msg):
                for server in bot.guilds:
                    for channel in server.channels:
                        try:
                            await channel.send(msg)
                        except Exception:
                            continue
                        else:
                            break

我希望程序将我的消息发送到该机器人当前所在的所有服务器。

例如:!你好,这是一条公告!

应该触发!hello之后的消息,以在其中的每台服务器上广播。

编辑:获得一些帮助后,我仍然遇到问题!现在的错误是,即使执行了命令也没有任何显示,如果再次执行该命令,则会出现错误:“命令广播已被注册。”

1 个答案:

答案 0 :(得分:1)

为什么要像这样在client.command内使用client.event

只需使用命令即可:

@client.command(pass_context=True)
async def hello(ctx, *, msg):
    for server in client.servers:
        for channel in server.channels:
            try:
                await client.send_message(channel, msg)
            except Exception:
                continue
            else:
                break

这会将消息发送到公会中的第一渠道,该机器人具有允许发送消息的权限。

对于将来的参考,请考虑升级到最新的API版本,因为不支持旧的API版本,您将很难获得帮助。对于新的API,代码如下所示:

@client.command()
async def hello(ctx, *, msg):
    for server in bot.guilds:
        for channel in server.channels:
            try:
                await channel.send(msg)
            except Exception:
                continue
            else:
                break

编辑:根据Patrick的评论,您的特定错误表明您使用的是Client而不是Bot,如果是,请使用@bot.command

相关问题