简体   繁体   中英

Setting valid prefixes for the discord bot in discord.py

So I was making a discord bot in discord.py and when I created a command that used to sets the bot's prefix, I came up with this idea: "How do I know whether a user entered a valid character for the bot's prefix?"
eg here are some valid characters for the bot's prefix: ',', '$', '%', '&'. etc...
and here are some invalid characters: '╒', 'Γ', '▓', 'µ', '╦', etc...

and here is my code:

@commands.command(aliases=['prefixSet']) # also I'm creating this command in a cog
@commands.has_permissions(administrator=True) # setting permission
    async def change_prefix(self, context, prefix):
        valid_prefix = ['!', '@', '#', '$', '%', '^', '&', '*','-', '_', '+', '=', '~']
        if prefix in valid_prefix:
            with open('prefixes.json', 'r') as f:
                prefixes = json.load(f)

            prefixes[str(context.guild.id)] = prefix

            with open('prefixes.json', 'w') as f:
                json.dump(prefixes, f, indent=4)
        else:
            context.send('Abnormal symbol for new prefix, try something else.')

Basically, I'm using if statement to check if the value (this value contain the character) of prefix argument exists in the valid_prefix list, if the character that the user entered does exist in the valid_prefix list then it will accept that character as a new prefix otherwise it won't.
But when I tested it with this '╒' character, it still accepts that character as a new prefix, so you guys have any solution to this?

Not sure if this will work but, You should probably have a return statement for your else so the bot will not change the prefix of the server if they don't give a valid prefix. So something like this MIGHT work. Also we don't say context , we say ctx .

@commands.command(aliases=['prefixSet']) # also I'm creating this command in a cog
@commands.has_permissions(administrator=True) # setting permission
    async def change_prefix(self, ctx, prefix):
        valid_prefix = ['!', '@', '#', '$', '%', '^', '&', '*','-', '_', '+', '=', '~']
        if prefix not in valid_prefix: #if they don't give a valid prefix
            await ctx.send('That is not a valid prefix!')
            return #the bot won't change the prefix if it's invalid
        if prefix in valid_prefix: #if they give a valid prefix
            with open('prefixes.json', 'r') as f:
                prefixes = json.load(f)

            prefixes[str(context.guild.id)] = prefix

            with open('prefixes.json', 'w') as f:
                json.dump(prefixes, f, indent=4)
            await ctx.send(f'Prefix changed to {prefix}')

I'm not 100% positive this will work, if you get any errors or have any questions, just feel free to ask:D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM