Discord.py(如果无法检查布尔值)

时间:2018-11-09 06:48:53

标签: python discord discord.py

这是我的小问题:

        if start == False:
          start = True
          players_list.append(ctx.message.author.id)
          await self.client.send_message(channel, '**Session created by {}** Wait another player to join..'.format(ctx.message.author))

开始boolean,如果if导致错误bcs,我将start = True放入if内

完整代码:

import discord
from discord.ext import commands

players_list = []
wolves_list = []
villagers_list = []

start = False

class Werewolf:

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

    @commands.command(pass_context = True)
    async def playww(self, ctx):
        channel = ctx.message.channel
        if start == False:
            start = True
            players_list.append(ctx.message.author.id)
            await self.client.send_message(channel, '**Session created by {}** Wait another player to join..'.format(ctx.message.author))
        else:
            await self.client.send_message(channel, '**Session already created!**')

    @commands.command(pass_context = True)
    async def joinww(self, ctx):
        channel = ctx.message.channel
        id_player = ctx.message.author.id
        if start == True:
            if id_player not in players_list:
                players_list.append(id_player)
                await self.client.send_message(channel, '**{}** joined the game!'.format(ctx.message.author))
            else:
                await self.client.send_message(channel, '**{}** already inside player list!'.format(ctx.message.author))
        else:
            await self.client.send_message(channel, 'Create session first by `-playww` command!')


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

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

使start(以及所有其他“全局”变量)成为Werewolf嵌齿轮的属性。

class Werewolf:

    def __init__(self, client):
        self.client = client
        self.players_list = []
        self.wolves_list = []
        self.villagers_list = []
        self.start = False