SoupChild REVAMPED
SoupChild REVAMPED

Reputation: 31

How to fix command not working multiple times

I have a command ¬host which initiates a game. A person with the role Admin can use ¬cleargames to stop the games and delete the messages, via a variable Host.

However, if I run ¬host and then ¬cleargames it works. If I do it again, I get an error.

This is for a discord server using discord.py asyncio. I keep getting the error:

Command cleargames is already registered.


@client.command(pass_context=True)
async def host(ctx):
  host = "."
  if host == ".":
    host = ctx.message.author
    message = (f"__**Player List**__ \n \n{host.mention} (Host)")
    playerList = await client.say(message)
    @client.command(pass_context=True)
    @has_role("Admin")
    async def cleargames(ctx):
      command = ctx.message
      await client.delete_message(playerList)
      await client.delete_message(command)
      notfication = await client.say("Games cleared.")
      time.sleep(5)
      await client.delete_message(notification)
      host = "."

It should be able to carry out the ¬host and ¬cleargames command multiple times, without getting the error.

Upvotes: 0

Views: 52

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 61062

You can't have multiple versions of the same command. When you try to run host a second time, it tries to register a command under the name cleargames again, which fails.

Instead, write two separate commands that share state through mutually accessed global variables.

playerList = None

@client.command(pass_context=True)
async def host(ctx):
    global playerList
    if playerList:
        return
    host = ctx.message.author
    message = (f"__**Player List**__ \n \n{host.mention} (Host)")
    playerList = await client.say(message)

@client.command(pass_context=True)
@has_role("Admin")
async def cleargames(ctx):
    global playerList
    if playerList:
        await client.delete_message(playerList)
        playerList = None
        await client.delete_message(ctx.message)
        notfication = await client.say("Games cleared.")
        await asyncio.sleep(5)
        await client.delete_message(notification)

Upvotes: 1

Related Questions