【问题标题】:How do i make a change prefix command in discord.py?如何在 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}")
    ```



【问题讨论】:

标签: json discord discord.py bots prefix


【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 2019-11-09
      • 1970-01-01
      • 2021-02-07
      • 2021-10-30
      • 2020-08-26
      • 2021-04-04
      • 2021-07-07
      • 1970-01-01
      • 2021-07-26
      相关资源
      最近更新 更多