从JSON获取服务器ID

时间:2018-10-06 14:32:38

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

我想从JSON文件中获取服务器ID,对此我感到困惑。下面是我当前的Python文件

import discord
from discord.ext import commands
import youtube_dl
import json

with open("configs/premium.json") as f:
    premium = json.load(f)

def premium(bot, message):
    id = message.server.id
    return premium.get(id)

class Play:
    def __init__(self, client):
        self.client = client

    @commands.command(pass_context=True)
    async def play(self, ctx):
        try:
            if ctx.message.server.id == premium:
                await self.client.say('Premium works')
            else:
                await self.client.say('Non-Premium Works!')
        except Exception as error:
            await self.client.say('{}'.format(error))

def setup(client):
    client.add_cog(Play(client))

JSON文件

{"serverID":"498054637245693952"}

1 个答案:

答案 0 :(得分:0)

如果您的JSON文件是

  

{“ serverID”:“ 498054637245693952”}

然后只需替换

with open("configs/premium.json") as f: premium = json.load(f)

使用

with open("configs/premium.json") as f: premium = json.load(f)['serverID']

此外,您应该放弃def premium(bot, message):函数或使用其他名称,因为它会干扰服务器ID变量名称,例如。

enter image description here