Discord.py 2.0中如何绕过commands.Bot()的必填命令前缀?
在Discord.py 2.0中绕过commands.Bot()的必填命令前缀
当然可以绕过这个必填项,这里有两种实用的方法:
- 设置无效前缀
直接给command_prefix传入一个用户不可能触发的值,比如空字符串或者特殊字符组合。示例代码:
from discord.ext import commands import discord # 空前缀写法 bot = commands.Bot(command_prefix="", intents=discord.Intents.all()) # 更稳妥的特殊字符前缀(几乎不会被用户输入) bot = commands.Bot(command_prefix="!@#$%^&*()", intents=discord.Intents.all())
- 使用
when_mentioned_or搭配空值
借助discord.ext.commands里的when_mentioned_or函数,既能保留“提及机器人触发命令”的功能(如果需要),又相当于没有常规前缀:
from discord.ext import commands import discord bot = commands.Bot(command_prefix=commands.when_mentioned_or(""), intents=discord.Intents.all())
如果你只需要使用斜杠命令、完全不需要传统前缀触发的命令,以上两种方法都能满足需求,同时让你享受到commands.Bot带来的命令分组、参数解析、内置斜杠命令支持等便利特性。
内容的提问来源于stack exchange,提问作者Bob
相关产品推荐
相关产品推荐

