Blake Xavier
Blake Xavier

Reputation: 85

Discord Bot - AttributeError: 'Client' object has no attribute 'commands'

I'm attempting to create a new Discord bot, and an issue occurs when trying to create a message that is announced to all Discord Servers the bot is currently in.

I have attempted to solve the problem to no avail, this includes looking it up, reading documentation, and of course trying new code.


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

I expect the program to send my message to all servers the bot is currently in.

Ex: !hello Hi there, this is an announcement!

Should trigger the message following !hello to be broadcasted on every server there is.

EDIT: After some help, I'm still having an issue! The error now is that nothing appears even after doing the command, and if I do it again, it comes up with an error: "Command broadcast is already registered."

Upvotes: 0

Views: 3433

Answers (1)

BrainDead
BrainDead

Reputation: 795

Why would you use a client.command inside of a client.event like that?

Just use command instead:

@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

This will send the message to the first channel in guild where the bot has permission to send messages.

For future references consider upgrading to the latest API version since old ones are NOT supported and you will get a hard time getting help. For the new API the code would look like this:

@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

Edit: As per comment from Patrick your specific error indicates that you're using Client instead of Bot , if so just use @bot.command instead.

Upvotes: 1

Related Questions