【问题标题】:Discord Python Rewrite - Custom Server Prefixes ErrorDiscord Python 重写 - 自定义服务器前缀错误
【发布时间】:2020-12-18 01:42:49
【问题描述】:

我试过这些代码

def get_prefix(client, message):
with open('prefixes.json', 'r') as f:
    prefixes = json.load(float)

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

client = commands.Bot(command_prefix = get_prefix)

@client.event
async def on_guild_join(guild):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(float)

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

    with open('prefixes.json', 'w') as f:
        json.dump(prefixes, float, indent=4)

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

    prefixes.pop(str(guild.id))

    with open('prefixes.json', 'w') as float:
        json.dump(prefixes, float, indent=4)

@client.command()
@commands.has_permissions(manage_channels=True)
async def 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, indent=4)

    await ctx.send(f'Prefix changed succesfully, now my prefix for this server is: "{prefix}"')

我得到了这些错误

忽略 on_message 中的异常 回溯(最近一次通话最后): _run_event 中的文件“C:\Users\PC\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py”,> 第 312 行 等待 coro(*args, **kwargs) 文件“C:\Users\PC\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py”,第 943 行,on_message 等待 self.process_commands(消息) 文件“C:\Users\PC\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py”,第 939 行,在 process_commands ctx = 等待 self.get_context(消息) 文件“C:\Users\PC\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py”,第 853 行,在 get_context 前缀 = 等待 self.get_prefix(消息) 文件“C:\Users\PC\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py”,第 798 行,在 get_prefix ret = await discord.utils.maybe_coroutine(前缀,自我,消息) 文件“C:\Users\PC\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\utils.py”,行 >331,在 maybe_coroutine 中 值 = f(*args, **kwargs) 文件“C:\Users\PC\Desktop\Code\Project 01\index.py”,第 20 行,在 get_prefix 返回前缀[str(message.guild.id)] 键错误:'744103149471662152'

请帮忙,几周前它还在工作。

【问题讨论】:

  • 您能解释一下json.load(float)json.dump(.., float,..) 的作用吗?你的意思是f
  • 我试着把它改成 F 到 Float 还是一样的错误

标签: python python-3.x discord.py discord.py-rewrite


【解决方案1】:
def get_prefix(client, message):
    with open('./database/prefixes.json', 'r') as f:
        prefixes = json.load(f)
    return prefixes[str(message.guild.id)]

您确实正确加载了float,而不是写入f 并检查 json 文件中的服务器 ID 尝试手动编辑或踢机器人并再次邀请它

【讨论】:

    【解决方案2】:

    好的。首先,您正在运行json.loads(float)。 Float 是一个函数,而不是文件。我编辑了您发布的代码,请尝试使用:

    def get_prefix(client, message):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)
    
        return prefixes[str(message.guild.id)]
    
    client = commands.Bot(command_prefix = get_prefix)
    
    @client.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, indent=4)
    
    @client.event
    async def on_guild_remove(guild):
        with open('prefixes.json', 'r') as f:
            prefixes = json.load(f)
    
        prefixes.pop(str(guild.id))
    
        with open('prefixes.json', 'w') as f:
            json.dump(prefixes, f, indent=4)
    
    @client.command()
    @commands.has_permissions(manage_channels=True)
    async def 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, indent=4)
    
        await ctx.send(f'Prefix changed successfully, now my prefix for this server is: "{prefix}"')
    

    (我还修复了您消息中的一些拼写错误)

    如果这仍然给您带来错误,请踢掉机器人并重新邀请。那么应该可以工作了。

    【讨论】:

    • 抱歉来晚了,我去试试
    【解决方案3】:

    服务器 ID 不在 JSON 文件中

    手动添加或从该服务器中踢出机器人并重新邀请它

    【讨论】:

      猜你喜欢
      • 2021-04-04
      • 2021-04-25
      • 2018-08-06
      • 1970-01-01
      • 1970-01-01
      • 2012-08-05
      • 2021-11-19
      • 2019-04-12
      • 2018-03-28
      相关资源
      最近更新 更多