如何忽略某些语音通道?不和谐

时间:2020-09-07 14:44:23

标签: discord.py

使代码每分钟得分1分。问题在于,在不同的语音通道中,不应发出分数,例如afk通道。

试图通过guild.afk_channel中的if成员来执行此操作,但是它无效:/

    @commands.Cog.listener()
    async def on_ready(self):
        while True:
            with open("./data/Points.json", "r") as file:
                data = json.load(file)

            for guild in self.bot.guilds:
                if str(guild.id) not in data.keys():
                    data[str(guild.id)] = {}

                voices = [channel for channel in guild.voice_channels]
                members = [channel.members for channel in voices]

                ids = []
                for lst in members:
                    for member in lst:
                        ids.append(member.id)      

                if len(ids) <= 0:
                    continue

                for member in ids:
                    if str(member) not in data[str(guild.id)].keys():
                        data[str(guild.id)][str(member)] = 0

                    elif member in self.prev:
                        data[str(guild.id)][str(member)] += 1

                    else:
                        self.prev.append(member)

            with open("./data/Points.json", "w") as file:
                json.dump(data, file, indent=4)

            await asyncio.sleep(60)

1 个答案:

答案 0 :(得分:0)

首先,我建议为此使用内置的task,因为它是在设定的时间表上反复执行的相同功能。

回答您的问题:

您可以创建要忽略的频道ID(或实际的channel实例)列表,为简单起见,我将其称为ignored_channels。然后,您可以从voices列表中过滤掉它们,这样您的for-loop就不会遍历它们,它们将被忽略。您可以这样做:

voices = [channel for channel in guild.voice_channels if channel not in ignored_channels]

或者,如果您使用id的另一种方法(我建议这样做,因为它可以更快,更容易实现):

voices = [channel for channel in guild.voice_channels if channel.id not in ignored_channels]

这样,将不会检查ignored_channels中的语音通道,而这正是您要达到的目的。