Получение идентификаторов сервера из JSON - PullRequest
0 голосов
/ 06 октября 2018

Я хочу получить идентификатор сервера из файла JSON, который я не совсем понимаю, как это сделать.Ниже мой текущий файл 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 голосов
/ 06 октября 2018

Если ваш файл 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): или использовать другое имя, поскольку оно влияет на имя переменной идентификатора вашего сервера, например

enter image description here

...