1
votes

A discord bot that I set up worked fine at first, but is now repeating messages (or sending multiple messages) after a command. I'm not sure as to what to do to fix this.

I'm trying to set up a bot for a discord server that I'm staff on. The staff team knows that I often look over bots online and that I can code somewhat. They asked me whether I could make a bot for the server, so I decided to give it a shot. After about 1.5 hours, I set up a bot and gave it some commands to work with. I tried searching online for solutions but couldn't find anything that fit the problem.

This is the main code I have in my 'bot.py' file. I hid the Bot token at client.run for obvious reasons.

import discord
import random
import os
from discord.ext import commands

client = commands.Bot(command_prefix = 'pj!')

@client.command()
async def load(ctx, extension):
    client.load_extension(f'cogs.{extension}')

@client.command()
async def unload(ctx, extension):
    client.unload_extension(f'cogs.{extension}')

for filename in os.listdir('./cogs'):
    if filename.endswith('.py'):
        client.load_extension(f'cogs.{filename[:-3]}')

client.run('[REDACTED]')

This is the code I have in a Cog titled Alpha.py (temporary title). At the moment, only the ping and help commands are giving problems.

import discord
from discord.ext import commands

class Alpha(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.Cog.listener()
    async def on_ready(self):
        print('Bot: Online')

    @commands.command()
    async def ping(self, ctx):
        await ctx.send(f'Pong! {round(client.latency * 1000)}ms')

    @commands.command()
    async def _8ball(self, ctx, *, question):
        responses = ['It is certain.',
                     'It is decidedly so.',
                     'Without a dobut.',
                     'Yes - definitely.',
                     'You may rely on it.',
                     'As I see it, yes.',
                     'Most likely.',
                     'Outlook good.',
                     'Yes.',
                     'Signs point to yes.',
                     'Reply hazy, try again.',
                     'Ask again later.',
                     'Better not tell you now.',
                     'Concentrate and ask again.',
                     "Don't count on it.",
                     'My reply is no.',
                     'My sources say no.',
                     'Outlook not so good.',
                     'Very doubtful.']
        await ctx.send(f'Question: {question}\nAnswer: {random.choice(responses)}')

def setup(client):
    client.add_cog(Alpha(client))

Ping Command For the ping command, I expect it to post a single message, "Pong! [latency]ms" once with a single value where [latency] is. Instead it posts the message three times, with three different values where [latency] is

Help Command For the help command, I expect it to post one single message showing the commands that are available. Instead it sends six messages (with some messages showing all the commands, and some only showing one or two).

1
It seems that you have multiple instances of your bot running at the same time. Try to close all your terminal and all the stuff that make your bot run, then retry. If your bot is running on a host like Heroku, change your token to end all the processes.DrLarck

1 Answers

0
votes

If you are hosting this bot on Heroku or any other host service and you are hosting the bot at your PC at the same time it will respond to messages twice (or even more if you host on even more servers). Try changing the bot's token to stop everything.