简体   繁体   English

从 on_message 获取公会 ID

[英]Get guild ID from on_message

I'm currently trying to make a server prefix system using json.我目前正在尝试使用 json 创建服务器前缀系统。

I'm trying to get the prefix from the prefixes.json file but I get this:我试图从 prefixes.json 文件中获取前缀,但我得到了这个:

错误图片

This is the on_message I'm using这是我正在使用的 on_message


@bot.event
async def on_message(message):
    if '<@754643724193235035>' == message.content:
        channel = message.channel
        a = open('prefixes.json', 'r')
        serverprefix = json.load(a)
        await channel.send("My prefix is:")
        await channel.send(serverprefix['message.guild.id'])
    await bot.process_commands(message)

This is the json I'm using这是我正在使用的 json


{
    "712329859562315886": ";",
    "709202272655118233": "~"
}

There's also been another thing bugging me, how could I add serverprefix['message.guild.id'] to af""?还有另一件事困扰着我,如何将 serverprefix['message.guild.id'] 添加到 af""?

The reason of the error is you try to get a value from a key named message.guild.id .错误的原因是您尝试从名为message.guild.id的键中获取值。 Obviously there is no key with that name.显然没有那个名字的钥匙。 So you have to use string fromatting.所以你必须使用string fromatting。

You should change await channel.send(serverprefix['message.guild.id']) to await channel.send(serverprefix[f'{message.guild.id}']) .您应该将await channel.send(serverprefix['message.guild.id'])更改为await channel.send(serverprefix[f'{message.guild.id}']) So you can get the prefix from json file.所以你可以从 json 文件中获取前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM