Alfredo Albélis
Alfredo Albélis

Reputation: 27

Is there a way to update the aliases of a bot discord command?

I have a command that sends text copypasta

example copypasta.json:

{
    "xxx" : "xxxxxx xxxx xxxxx xxxxx...",
    "yyy" : "yyyyyy yyyy yyyyy yyyyy...",
    "zzz" : "zzzzzz zzzz zzzzz zzzzz..."
}

Code to send the text:

json_file = 'copypasta.json'
with open(json_file) as json_data:
    jsonLoad = json.load(json_data)

aliases = list(jsonLoad.keys())

@client.command(aliases=aliases) #problem is here
async def _copypasta(ctx):

    keyCopypasta = ctx.invoked_with
    valueCopypasta = jsonLoad[keyCopypasta]

    await ctx.send(valueCopypasta)

If i send -xxx in Discord, the bots sends the value in json "xxxx xxx..."

So I made a command to add a new element in json:

async def addCopypasta(ctx, key, *, value):
    
    a_dictionary = {key: value}

    with open("copypasta.json", "r+") as file:
        data = json.load(file)
        data.update(a_dictionary)
        file.seek(0)
        json.dump(data, file)
    
    await ctx.send("successfully added")

But when I send in Discord the key of the new element added, the bot does not find it, I need to restart the bot so that the command's "aliases" variable is updated.

Is it possible to update the command aliases without restarting the bot?

Upvotes: 1

Views: 152

Answers (2)

Leonardo Ferrari
Leonardo Ferrari

Reputation: 23

Yes, it's possible to update the command aliases without restarting the bot. In Discord.py, you can achieve this by dynamically adding and removing aliases to your command after the bot is already running:

from discord.ext import commands

bot = commands.Bot(command_prefix='!')

# Your existing code here...

# Add a new alias dynamically
@bot.command()
async def add_alias(ctx, new_alias):
   command_name = '_copypasta'

   command = bot.get_command(command_name)
   command.aliases.append(new_alias)

   # Send a message to the user
   await ctx.send(f'Added new alias: {new_alias} for {command_name}')

# Remove an alias dynamically
@bot.command()
async def remove_alias(ctx, alias_to_remove):
   command_name = '_copypasta'

   command = bot.get_command(command_name)

   if alias_to_remove in command.aliases:
       command.aliases.remove(alias_to_remove)
       await ctx.send(f'Removed alias: {alias_to_remove} from {command_name}')
   else:
       await ctx.send(f'{alias_to_remove} is not an alias for {command_name}')

bot.run('YOUR_BOT_TOKEN')

Upvotes: 1

Łukasz Kwieciński
Łukasz Kwieciński

Reputation: 15728

It is possible, simply remove the command, update the aliases and add the command again, a handy function would be:

def update_aliases(command, *aliases):
    client.remove_command(command.name)
    command.aliases.extend(aliases)
    client.add_command(command)

To use it:

@client.command()
async def foo(ctx):
    await ctx.send(foo.aliases)

@client.command()
async def update(ctx, alias: str):
    update_aliases(foo, alias) # I'm passing the function itself, not the name of the function
    await ctx.send("Done")

Upvotes: 0

Related Questions