首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在discord.py中创建更改前缀命令?

如何在discord.py中创建更改前缀命令?
EN

Stack Overflow用户
提问于 2021-07-07 08:59:10
回答 2查看 719关注 0票数 0

因此,我按照教程编写了一个前缀命令,但是当我试图更改前缀时,我什么也得不到。也没有错误。我尝试使用默认前缀运行其他命令,但它们也没有工作。我使用的代码是:

代码语言:javascript
复制
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}")
    ```
EN

回答 2

Stack Overflow用户

发布于 2021-07-07 10:31:22

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

这里描述的原因是^

代码语言:javascript
复制
@bot.event
async def on_message(msg):
    await bot.process_commands(msg) # add this line
票数 0
EN

Stack Overflow用户

发布于 2021-07-07 10:51:33

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

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68283032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档