如何在 discord.py 中进行更改前缀命令?

Posted

技术标签:

【中文标题】如何在 discord.py 中进行更改前缀命令?【英文标题】:How do i make a change prefix command in discord.py? 【发布时间】:2021-09-17 19:44:31 【问题描述】:

所以,我按照教程制作了更改前缀命令,但是当我尝试更改前缀时,我什么也没得到。也没有错误。我尝试使用默认前缀运行其他命令,但它们也不起作用。我使用的代码是:

import json

def get_prefix(bot,message):

        with open ("prefixes.json", "r") as f:
                prefixes = json.load(f)

        return prefixes[str(message.guild.id)]

bot = commands.AutoShardedBot(command_prefix = get_prefix, help_command=None, intents=discord.Intents.all())


@bot.event
async def on_guild_join(guild):

        with open ("prefixes.json", "r") as f:
                prefixes = json.load(f)

        prefixes[str(guild.id)] = ","

        with open ("prefixes.json", "w") as f:
                json.dump(prefixes,f)


@bot.command()
@commands.has_permissions(administrator = True)
async def change_prefix(ctx, prefix):
        with open ("prefixes.json", "r") as f:
                prefixes = json.load(f)

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

        with open ("prefixes.json", "w") as f:
                json.dump(prefixes,f)

@bot.event
async def on_message(msg):
        if msg.mentions == bot.user:
        
                with open ("prefixes.json", "r") as f:
                        prefixes = json.load(f)
                pre =  prefixes[str(msg.guild.id)]
                await msg.channel.send(f"My Prefix is pre")
    ```



【问题讨论】:

这对您有帮助吗? Discord.py variables not constant throughout the code 你能显示你的 JSON 文件吗? @Abdulaziz 我的 JSON 文件是空白的 【参考方案1】:

您需要将 process_commands(message) 添加到您的 on_message 事件中

那里描述了为什么会这样^^^^

@bot.event
async def on_message(msg):
    await bot.process_commands(msg) # add this line

【讨论】:

【参考方案2】:

@Guddi 现在,当我尝试将前缀更改为“!”时,会出现以下错误:

Ignoring exception in on_message
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 55, in on_message
    await bot.process_commands(msg)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 975, in process_commands
    ctx = await self.get_context(message)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 886, in get_context
    prefix = await self.get_prefix(message)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 831, in get_prefix
    ret = await discord.utils.maybe_coroutine(prefix, self, message)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/utils.py", line 341, in maybe_coroutine
    value = f(*args, **kwargs)
  File "main.py", line 23, in get_prefix
    prefixes = json.load(f)
  File "/usr/lib/python3.8/json/__init__.py", line 293, in load
    return loads(fp.read(),
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

【讨论】:

以上是关于如何在 discord.py 中进行更改前缀命令?的主要内容,如果未能解决你的问题,请参考以下文章

前缀更改后重新加载不和谐机器人 (Discord.py)

(discord.py) 如何使我的 setprefix 命令正常工作?

discord.py,同时使用斜杠命令和前缀

在 discord.py 中显示设置前缀的命令(重写)

如何在 discord.py 库中设置多个前缀或使前缀不区分大小写?

discord.py前缀命令[关闭]