簡體   English   中英

Python discord 機器人檢查所有用戶的特定角色,該角色也具有特定角色

[英]Python discord bot to check all users for a specific role that also HAVE a specific role

好的,所以我正在 python 中編寫我的第一個 Discord 機器人,但我在查找如何准確執行此操作時遇到了麻煩。 我將結合 python、視覺基本命令(因為我比 python 更了解這些命令)和簡單的英語來編寫我需要它做的事情,以便更好地了解我想要完成的工作。

請注意,這個 python 機器人在我的 PC 上運行,而不是托管。 它僅在一個(我的)discord 服務器中。

@client.command()
async def team1(ctx):
     await ctx.send('Sending Inactivity Report For Team1')

#Here's where I combine languages just to show what I'm trying to do

users = (Every member in server)

For each users
     if user has joined in 21 days or less then
          goto skip
     end if

     if user has role "Absent With Notice" then
          goto skip
     end if
     if user has role "Team1" then
          if user does NOT have role "Active" then
               await ctx.send('User is inactive and should be warned.')
          end if
     end if
skip:
next users

關於從哪里開始的任何想法? 任何幫助都是極好的。 :)

您可以遍歷公會的所有成員,獲取一個角色,然后檢查該角色是否在成員的角色菜單中,如下所示:

role = discord.utils.get(ctx.guild, id = your_role_id)

for member in ctx.guild.members:
    if role in member.roles:
        #do things

在此處查看文檔: https://discordpy.readthedocs.io/en/latest/api.html

你實際上可以很容易地做到這一點。 因此,我將逐步添加代碼並構建命令。

讓我們開始。

第 1 步:創建具有一些權限限制的 function。

這里將定義 function 並限制其使用。 現在我們將只設置Administrator權限。 您可以隨時更改它並添加任何權限,例如: manage_rolesmanage_channels或 discord 的任何權限。

您需要先編寫此命令和其他導入命令。

from discord.ext import commands

(如果您已經在代碼中添加了它,請忽略它。)

在這里,我們用附加的文本消息定義了它,說明它正在啟動:

@ndcr.command()
@commands.has_permissions(administrator=True)
async def rolecheck(ctx):
    await ctx.channel.send("Acquiring Inactivity Report!")

現在我們已經定義了這個,讓我們進入下一步。

第 2 步:為所需的角色定義變量。

根據您的要求,我假設您已經擁有這些角色,因此我根據它們的名稱定義了這些角色。 那么名稱可以隨時更改,因此請根據您使用的服務器進行更改。

以下是定義的角色:

joined_in_21_days_or_before_role = discord.utils.get(ctx.guild.roles, name='ADD THE NAME OF THIS ROLE')
absent_with_notice_role = discord.utils.get(ctx.guild.roles, name='Absent With Notice')
team_role = discord.utils.get(ctx.guild.roles, name="Team1")
active_role = discord.utils.get(ctx.guild.roles, name="Active")

在第一個中,我不確定名稱是什么,所以自己添加。

第 3 步:創建並清空列表以添加成員以進行顯示。

此列表將包含具有“活動”角色的成員。 這里是:

memberlist = []

第 4 步:創建一個包含內容的循環。

根據您的要求,我創建了一個循環遍歷每個成員並找到所需的 output。

首先,它檢查成員是否具有“加入 21 天或更少”的角色。 如果他們有它,代碼只是傳遞並移動到下一個成員。

其次,它檢查角色"Absent With Notice" 如果他們具有此角色,則代碼將傳遞給另一個用戶並且什么也不做。

第三,它檢查用戶是否具有“Team1”角色。 如果他們確實有它,那么如果里面有內容並且沒有else條件。 因此,如果該成員具有“Team1”角色,那么它會檢查另一個角色“Active”。 如果用戶有這個角色,腳本會將他們添加到我們memberlist創建的成員列表中。 這里也沒有else條件。 如果需要,您可以自己添加它們。 只需將中間if s 更改為elif就可以了。

這是代碼:

for person in ctx.guild.members:
    if joined_in_21_days_or_before_role in person.roles:
        pass
    
    if absent_with_notice_role in person.roles:
        pass

    if team_role in person.roles:
        if active_role not in person.roles:
            memberlist.append(f"{person.name}")
    
await ctx.channel.send(f"Succesfully Completed the Process. \nThe following is the list of Inactive Members. Such a Disgrace. >:( \n \n{memberlist}")

這對我有用,因為我自己嘗試過。 這里有一些截圖來證明它確實有效,並向您展示它是如何工作的。

命令如何工作。


這是你想用你的代碼做什么的最簡單的實際方法。 希望對您有所幫助,如果您遇到任何錯誤,請在評論中告訴他們。 :)

謝謝::D

暫無
暫無

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

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