Ban a user if they leave the server when they have a specific role discord.py(如果用户在具有特定角色时离开服务器,则禁止用户 discord.py)
问题描述
我想这样做,如果用户在具有 Muted
或 [Banned]
角色时离开服务器,他们将被永久禁止.
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
->服务器成员意图
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)
这篇关于如果用户在具有特定角色时离开服务器,则禁止用户 discord.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果用户在具有特定角色时离开服务器,则禁止用户 discord.py


基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01