prune
prune

Reputation: 79

Confused on how to make discord bot send a certain amount of messages?

I'm trying to make a small spambot with discord.js, as one of my first python bots. It honestly works fine, but there's one problem. The bot is supposed to spam every channel in a server, but it only sends one message. I don't know python that well, and I know where the problem is and what I need to do to fix it, I just don't know how to fix it. I'm guessing I need to put an amount of messages that I want the bot to send, but I don't know how to do that. I'm hoping someone can help! (P.S. I'm not using the bot for anything bad, I just wanna test it out.) Anyway, here's my code:

@bot.command()
async def sall(ctx, *, message=None):
    if message == None:
        for channel in ctx.guild.channels:
            try:
                await channel.send(random.choice(spam_messages))
            except discord.Forbidden:
                print(f"{C.RED}Spam Error {C.WHITE}[Cannot send messages]")
                return
            except:
                pass
    else:
        for channel in ctx.guild.channels:
            try:
                await channel.send(message)
            except discord.Forbidden:
                print(f"{C.RED}Sall Error {C.WHITE}[Cannot send messages]")
                return
            except:
                pass

Upvotes: 1

Views: 502

Answers (1)

Malware
Malware

Reputation: 89

Well, you really only send one message!

@bot.command()
@bot.is_owner()
async def sall(ctx, *, message=None):
    if message == None:
        for channel in ctx.guild.channels:
            try:
                for i in range(10):
                    await channel.send(random.choice(spam_messages))
            except discord.Forbidden:
                print(f"{C.RED}Spam Error {C.WHITE}[Cannot send messages]")
                return
            except:
                pass
    else:
        for channel in ctx.guild.channels:
            try:
                for i in range(10):
                    await channel.send(message)
            except discord.Forbidden:
                print(f"{C.RED}Sall Error {C.WHITE}[Cannot send messages]")
                return
            except:
                pass

It should work like this. I added a for i in range(10), what it does is, it repeats this 10 times. So just change it to number of times you want it to send a message.

Upvotes: 1

Related Questions