【问题标题】:Get_prefix discord bot error (async function wont work)Get_prefix discord bot 错误(异步功能不起作用)
【发布时间】:2021-08-24 02:59:47
【问题描述】:

我正在制作一个自定义前缀命令,该命令使用 JSON 来获取所述服务器的前缀,但由于某种原因它无法正常工作。我认为正在发生的是我的异步功能正常工作,但我无法弄清楚发生了什么。

代码 这是没有不需要的不和谐机器人的代码


async def get_prefix(client, message):
    with open('storage\pp.json', 'r') as f:
        prefixes = json.load(f)


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



#Defining Bot:
Bot = commands.Bot(command_prefix= get_prefix)


@Bot.event
async def on_guild_join(guild):
    with open('storage\pp.json', 'r') as f:
        prefixes = json.load(f)


    prefixes[str(guild.id)] = '.'

    with open('storage\pp.json', 'w') as f:
        json.dump(prefixes, f, indent=4)
        print(f'{guild.id} Joined')



@Bot.event
async def on_guild_remove(guild):
    with open('storage\pp.json', 'r') as f:
        prefixes = json.load(f)


    prefixes.pop(str(guild.id))

    with open('storage\pp.json', 'w') as f:
        json.dump(prefixes, f, indent=4)
        print(f'{guild.id} Left')

#the command

@Bot.command(pass_context = True)
@commands.has_permissions(administrator=True)
async def prefix(ctx, prefix):
    with open('storage\pp.json', 'r') as f:
        prefixes = json.load(f)


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

    with open('storage\pp.json', 'w') as f:
        json.dump(prefixes, f, indent=4)
        await ctx.send(f'***Prefix has been changed to***  ✅  ``{prefix}``')

这是我尝试运行时的错误。

return await value
  File "c:\Users\beaty\Desktop\junk\code\python\pinky\bot.py", line 101, in get_prefix
    return prefixes[str(message.guild.id)]
KeyError: '67685146592647##' #I took out two letters of that key because I don't know what it's referring to.

完全错误:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\beaty\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\beaty\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 979, in on_message
    await self.process_commands(message)
  File "C:\Users\beaty\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 975, in process_commands
    ctx = await self.get_context(message)
  File "C:\Users\beaty\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 886, in get_context
    prefix = await self.get_prefix(message)
  File "C:\Users\beaty\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 831, in get_prefix
    ret = await discord.utils.maybe_coroutine(prefix, self, message)
  File "C:\Users\beaty\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\utils.py", line 343, in maybe_coroutine
    return await value
  File "c:\Users\beaty\Desktop\junk\code\python\pinky\bot.py", line 101, in get_prefix
    return prefixes[str(message.guild.id)]
KeyError: '6768514659264758##'

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    在那里使用async 函数没有任何问题(但老实说,它没用,因为没有任何async 关于它),如果您尝试使用普通函数,错误不会神奇地消失。如果实际查看您的 JSON 文件,您不会看到 6768514659264758## 键,您需要使用 try/except 块并设置默认前缀。

    async def get_prefix(client, message):
        with open('storage\pp.json', 'r') as f:
            prefixes = json.load(f)
    
        try:
            prs = prefixes[str(message.guild.id)]
        except KeyError:
            prs = "your default prefix"
        return prs
    

    您可以使用dict.get 方法缩短代码:

    async def get_prefix(client, message):
        with open('storage\pp.json', 'r') as f:
            prefixes = json.load(f)
        return prefixes.get(str(message.guild.id), "your default prefix")
    

    【讨论】:

    • 它可以工作,但是当我将它上传到 heroku 时,我的路径有问题
    猜你喜欢
    • 2021-07-15
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2021-05-26
    • 1970-01-01
    • 2021-01-28
    • 2021-12-07
    • 2018-08-25
    相关资源
    最近更新 更多