【问题标题】:Iterate through every discord channel and find if a certain channel exists discord.py遍历每个不和谐频道并查找某个频道是否存在 discord.py
【发布时间】:2020-10-07 06:01:56
【问题描述】:

我想创建一个票务系统,所以我创建了一个 cog 并开始迭代 通过每个频道查看频道名称是否与用户的票务频道名称匹配所以有 不会有任何重复。

        for guild in client.guilds:
            for c in guild.channels:
                if str(c) == f"{username}-ticket":
                    room_exists = True
                    break
                else:
                    room_exists = False
        if room_exists:
            print(room_exists)
            return await ctx.send('You already have an open ticket!')
        if not room_exists:
            return await channel.send(f'{user_tag} Created this ticket, reason: {reason}')

由于某种原因,我无法确定机器人会创建一个房间并发送“您已经有一张未结票!”消息即使 room_exists == True

【问题讨论】:

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


    【解决方案1】:

    遍历每个渠道并不是优化的解决方案。你应该使用discord.utils.get。

    如果discord.utils.get 找不到频道,则返回None。在这里你可以做什么:

    ticket_channel = discord.utils.get(ctx.guild.text_channels, name=f'{username}-ticket')
    if ticket_channel:
        print(ticket_channel)
        await ctx.send('You already have an open ticket!')
        return
    else:
        await channel.send(f'{user_tag} Created this ticket, reason: {reason}')
        return
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-30
      相关资源
      最近更新 更多