簡體   English   中英

Discord.py 查找通道

[英]Discord.py find channel

我正在嘗試在 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”的通道,因為我在構建中使用了通道。

有沒有辦法可以修復它,如果通道不存在,則只發送一次,如果存在則只發送一次?

您可以隨時創建頻道。 所以用戶不需要手動創建它。 首先防止您遇到的錯誤發生。

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 - 您發送消息,如果不是,請對找到的通道執行您需要執行的操作。

首先,我將幫助您解決檢查通道是否存在的問題。 您可以在此處查看如何使用any 這是一種遍歷公會頻道的快速方法,如果任何頻道具有該名稱,它將滿足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 ,則返回頻道 object,這是之前的代碼沒有做的事情。

# 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}!")

以上代碼工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM