【问题标题】:Creating a channel in specific category (the category id is should be variable) discord.py在特定类别中创建频道(类别 id 应该是可变的) discord.py
【发布时间】:2021-09-15 00:25:45
【问题描述】:
maincategory = discord.utils.get(guild.categories, id=858441350667698190)

所以这是正常的方法,但我想通过提供变量来做到这一点:

wanted_category_id = 858441350667698190
maincategory = discord.utils.get(guild.categories, id=wanted_category_id)

但是当我print(maincategory) 它说None 我该怎么做?

【问题讨论】:

  • 你是如何得到分类的ID的?
  • 尝试打印guild

标签: discord.py


【解决方案1】:
category = bot.get_channel(wanted_category_id)
brand_new_channel = await category.create_text_channel(  # There's also create_voice_channel and create_stage_channel.
    channel_name,                                        # channel_name should be "general", not "#general".
    # The rest are optional.
    overwrites={                                         # Used for custom channel permissions and private channels.
        guild.default_role: discord.PermissionOverwrite(
            send_messages=False                          # By default, members can't send messages...
        ),
        guild.me: discord.PermissionOverwrite.from_pair(
            discord.Permissions.all(),                   # But the bot has all permissions enabled...
            discord.Permissions.none()                   # And none disabled!
        )
    },
    reason='Because I can'                               # This shows up in the audit logs.
)
await brand_new_channel.send('Ooh boy! A shiny new channel!')

重要部分:

await category.create_text_channel(channel_name)

【讨论】:

    【解决方案2】:

    你可以这样做

    c = discord.utils.get(ctx.guild.categories, id=ID) # Gets the category.
    await ctx.guild.create_text_channel(name="whatever", category=c)
    

    【讨论】:

      【解决方案3】:

      如果其他人遇到同样的问题:

      导致utils.get 或任何get_x 方法返回None 的常见情况是:

      • 机器人无法“看到”对象
      • 传递的 id 无效(ID 是整数,而不是字符串)
      • 对象不在缓存中或机器人未登录。

      一种可能的解决方案是使用 fetch_x 方法,该方法调用 API 以提取相关对象(无论它是否在缓存中)。

      wanted_category_id = 858441350667698190
      
      category = await bot.fetch_channel(wanted_category_id)
      

      Bot.fetch_channel

      【讨论】:

        猜你喜欢
        • 2021-05-30
        • 1970-01-01
        • 2020-10-21
        • 1970-01-01
        • 2020-11-17
        • 2020-12-07
        • 1970-01-01
        • 1970-01-01
        • 2021-06-03
        相关资源
        最近更新 更多