問題描述
我想這樣做,如果用戶在具有 Muted
或 [Banned]
角色時離開服務(wù)器,他們將被永久禁止.
I want make it so if a user leaves the server while they have the Muted
or [Banned]
role they get permanently banned.
這是我嘗試過的代碼:
@bot.event
async def on_member_remove(ctx, member, reason=None):
role="[Banned]"
guild = ctx.guild
if role in member.roles:
await guild.ban(discord.Object(id=member.id), reason="Leaved the server when soft banned")
*這只是一個嘗試,只有被禁止的角色.
*this is just a try with only the banned role.
用戶沒有被禁止,也沒有錯誤或任何可以幫助我解決問題的東西.
The user doesn't get banned, there is also no error or anything that could help me troubleshoot it.
推薦答案
member.roles 返回一個列表 角色
您需要獲取 Role 對象,您可以使用的一種方法是:
You need to get the Role object which one way you can use is:
role = discord.utils.find(lambda r: r.name == '[Banned]', member.guild.roles)
on_member_remove 接受會員.你不能有 reason
或 Context(ctx
)
on_member_remove takes in Member. You cannot have reason
or Context(ctx
)
@bot.event
async def on_member_remove(member):
role = discord.utils.find(lambda r: r.name == '[Banned]', member.guild.roles)
guild = member.guild
if role in member.roles:
await guild.ban(discord.Object(id=member.id), reason="Leaved the server when soft banned")
還請確保您啟用了成員意圖.你可以通過here 然后選擇 Bot
->服務(wù)器成員意圖
Please also ensure you have the Members intent enabled. You can do this by going here then selecting Bot
-> SERVER MEMBERS INTENT
您需要在代碼中啟用意圖,方法是:
You will need to do enable intents in your code by using:
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix=".", intents=intents)
這篇關(guān)于如果用戶在具有特定角色時離開服務(wù)器,則禁止用戶 discord.py的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!