【问题标题】:Discord.py find channelDiscord.py 查找频道
【发布时间】:2021-06-11 12:04:31
【问题描述】:

我正在尝试在 discord 中创建帮助“日志”命令。

@client.command()
async def logs(ctx):
 for channel in ctx.guild.channels:
  if channel.name == "henry-logs":
     await ctx.send("Henry Logs channel is already setup. If you have access to it, it should be available in the channel list")
  else:
   await ctx.send("Henry logs channel is not found! If you have such access please create channel named **EXACTLY**")
   await ctx.send("```henry-logs```")

但是它不会发送一次未找到的消息,而是针对每个不是“henry-logs”的频道,因为我在构建中使用了频道。

有没有办法修复它,如果频道不存在则只发送一次,如果存在则只发送一次?

【问题讨论】:

  • 给出的答案是否回答了您的问题?如果是这样,请将其标记为已接受的答案以关闭此问题。如果您仍然看到一些错误或任何意外行为,请在您的问题中提供更多详细信息或对答案发表评论,谢谢!

标签: python discord discord.py


【解决方案1】:

您可以随时创建频道。所以用户不需要手动创建它。首先防止您遇到的错误发生。

channel = await ctx.guild.create_text_channel('henry-logs')

创建频道后,您可以分配权限,例如:

await channel.set_permissions(ctx.guild.default_role, send_messages=False)

这将阻止任何非管理频道权限的成员在频道中发送消息。


更新

对不起,我现在明白你的问题了:

只需打破 for 循环并设置一个临时通道变量:

tempchan = null
for channel in ctx.guild.channels:
  if channel.name == "henry-logs":
     await ctx.send("Henry Logs channel is already setup. If you have access to it, it should be available in the channel list")
     tempchan = channel
     break;
if tempchan == null:
  await ctx.send("Henry logs channel is not found! If you have such access please create channel named **EXACTLY**")
   await ctx.send("```henry-logs```")
  

如果变量为 null - 您发送消息,如果不是,请对找到的频道执行您需要执行的操作。

【讨论】:

  • 这很好,但它仍然会为每个不使用该名称的频道创建一个新频道。例如,有 5 个不匹配的通道,它将创建 5 个通道。这是我的主要问题。
  • 对不起!检查我的最新编辑,我完全误解了你的问题。 @DeyanNikolov
  • 几乎一样。它打印出 4 次未找到和 1 次找到。
  • 你是否删除了 else 语句?
  • 我复制并粘贴了您的信息。
【解决方案2】:

首先我会帮你解决检查频道是否存在的问题。你可以看看如何使用any here。这是一种快速迭代公会频道的方法,如果任何频道具有该名称,它将满足if 语句并返回True

@client.command()
async def logs(ctx):
    if any("henry-logs" in channel.name for channel in ctx.guild.channels): # if there's any henry-logs channels, it will return True
        await ctx.send("Henry Logs channel is already setup. If you have access to it, it should be available in the channel list")
    else:
        await ctx.send("Henry logs channel is not found! If you have such access please create channel named **EXACTLY**")
        await ctx.send("```henry-logs```")

如果你不喜欢上面的压缩代码,下面的代码也是一样的。 bot 遍历公会中的所有频道,如果可以找到频道henry-logs,则返回一个频道对象,这是之前的代码没有做的。

# replaced if statement
channel_found = None
for channel in ctx.guild.channels:
    if "henry-logs" in channel.name:
        channel_found = channel
        break
if channel_found != None:
    await ctx.send(f"{channel_found.mention} is already setup.")
else:
    await ctx.send("Henry logs channel is not found! If you have such access please create channel named **henry-logs**")


如果您想自动创建频道,可以将 else 语句中的内容替换为以下内容。

@client.command()
async def logs(ctx):
    # this uses the first if-else statement I had shown!
    if any("henry-logs" in channel.name for channel in ctx.guild.channels):
        await ctx.send("Henry Logs channel is already setup. If you have access to it, it should be available in the channel list")
    else:
        await ctx.send("Henry logs channel is not found! I'm creating a channel now~")
        try:
            channel = await ctx.guild.create_text_channel('henry-logs')
        except: # raised if the bot doesn't have sufficient permissions
            await ctx.send("I couldn't create a `henry-logs` channel.. please make one for me, thanks!")
            return
        await ctx.send(f"I successfully created the channel {channel.mention}!")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    • 2018-04-04
    • 2021-02-27
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    相关资源
    最近更新 更多