python - 我可以发出命令关闭只有受信任的成员才能使用的不和谐机器人吗?[Python/DISCORD.PY]
问题描述
我们尝试了很多解决方案,但没有任何效果。这就是我们目前所拥有的(出于安全原因删除了个人详细信息)。
@bot.command(aliases=['disconnect', 'close', 'stopbot'])
async def logout(ctx):
if ctx.guild.id is not serverIdHere:
return
else:
role = discord.utils.get(ctx.guild.roles, name="Trusted Developer")
if role in ctx.author.roles:
await ctx.send(f"Ok")
await bot.logout()
else:
return
任何帮助将不胜感激。
解决方案
如果您想要具有特定角色的用户
@bot.command()
@commands.has_role('id_here or name')
async def logout(ctx):
await ctx.send('Shutting down')
await bot.logout()
如果您愿意,只有所有者才能使用它
@bot.command()
@commands.is_owner()
async def logout(ctx):
await ctx.send('Shutting down')
await bot.logout()
如果你想要特定的用户 ID,有三个选项
1.
def is_valid_user(ctx):
return ctx.author.id in my_list_of_ids
@bot.command()
@commands.check(is_valid_user)
async def logout(ctx):
await ctx.send('Shutting down')
await bot.logout()
def can_use_this_command():
async def predicate(ctx):
return ctx.author.id in my_list_of_ids
return commands.check(predicate)
@bot.command()
@can_use_this_command()
async def logout(ctx):
await ctx.send('Shutting down')
await bot.logout()
@bot.command()
async def logout(ctx):
if ctx.author.id not in my_list_if_ids:
return
await ctx.send('Shutting down')
await bot.logout()
参考:
推荐阅读
- java - 将动态参数传递给 @MapKey("${dynamicParam}") (My-Batis)
- c - 二维数组:为什么当我将 ptr 声明为 int ptr[4][4]; 时 ptr 和 *ptr 相同?
- c# - 我的程序在 client.DetectDocumentText(image) GoogleVisionAPI 处没有得到响应
- python - 在烧瓶服务器本身中运行 yolo 模型。如何?
- java - 如何在 JavaFx 中延迟加载图像?
- c# - 默认构造函数的目的
- javascript - 如何使用映射反应引导卡设置网格布局?
- c# - “无法评估表达式,因为代码已优化或本机框架位于堆栈顶部”同时将网格导出到 excel
- android - 即时应用中的搜索视图。Intent 在三星设备/模拟器中不触发
- javascript - 在我按下重生后,我想增加 10 到 2 个总数。不工作