0

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."

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

The only thing I would add to this is that the error they're seeing indicates that they're using Client instead of Bot
Ye just saw that the error is in the title :/ thanks for the heads up @PatrickHaugh
@PatrickHaugh If I use 'Bot' , I get: "NameError: name 'Bot' is not defined" I have client defined as: "client = discord.Client()" This should work right? I still get the same error :(
No. Use from discord.ext.commands import Bot, then client = Bot("!")
@PatrickHaugh Thank you for trying to help, I'm still struggling! I have updated my original post with the full script with all the changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.