等待错误会继续创建多个渠道,而不是进行检查

问题描述

嗨,我遇到了一个问题,我的代码创建了多个渠道。该代码功能应:

  1. 检查该频道是否存在
  2. 如果频道确实存在,请不要创建新频道。
  3. 否则,如果该频道不存在,请创建一个频道。
简而言之,on_message事件在发送给机器人的直接消息中检查用户的响应,然后将他们的消息中继到已经存在的公会渠道。在发送消息之前会创建一个新消息。

这是问题所在,我无法通过检查通道是否存在来解决它,它确实存在,但是每次发送消息时它都会重复:

我尝试了这两种方法进行检查:

#Check 1

for channel in guild.text_channels:
                if channel.name == f"{message.author.name.lower()}{message.author.discriminator}" and channel.name is not None:
                    await channel.send(embed=embed)
                else:
                    channel_non = await guild.create_text_channel(f'{message.author.name}{message.author.discriminator}',overwrites=overwrites,category=self.bot.get_channel(744944688271720518))
                await channel_non.send(embed=embed)


#Check 2

 channel_present = False
            for channel in guild.text_channels:
                if channel.name == f"{message.author.name.lower()}{message.author.discriminator}":
                    await channel.send(embed=embed)
                    channel_present = True

                if channel_present:
                    channel_non = await guild.create_text_channel(f'{message.author.name}{message.author.discriminator}',category=self.bot.get_channel(744944688271720518))
                await channel_non.send(embed=embed)

check 2代码导致on_message事件不执行任何操作。非常感谢您的帮助,我不知道我在哪里出问题了。

解决方法

老实说,我只是用命令来做。

@client.command()
async def command(ctx):
    author = ctx.message.author
    guild = ctx.guild

    for channel in guild.text_channels:
        if channel.name == ctx.message.author:
            await channel.send(embed = embed)
            return
    channel = await guild.create_text_channel(f"{ctx.message.author}",overwrites = overwrites,category = self.bot.get_channel(744944688271720518))
    await channel.send(embed = embed)