python - Discord.py 禁止命令
问题描述
if message.content.upper().startswith('!BAN'):
if "449706643710541824" in [role.id for role in message.author.roles]:
await
我有基本设置,所以只有管理员可以禁止。我想发出禁令命令,但我不知道该怎么做。
解决方案
试试这个:
import discord #Imports the discord module.
from discord.ext import commands #Imports discord extensions.
#The below code verifies the "client".
client = commands.Bot(command_prefix='pb?')
#The below code stores the token.
token = "Your token"
'''
The below code displays if you have any errors publicly. This is useful if you don't want to display them in your output shell.
'''
@client.event
async def on_command_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send('Please pass in all requirements :rolling_eyes:.')
if isinstance(error, commands.MissingPermissions):
await ctx.send("You dont have all the requirements :angry:")
#The below code bans player.
@client.command()
@commands.has_permissions(ban_members = True)
async def ban(ctx, member : discord.Member, *, reason = None):
await member.ban(reason = reason)
#The below code unbans player.
@client.command()
@commands.has_permissions(administrator = True)
async def unban(ctx, *, member):
banned_users = await ctx.guild.bans()
member_name, member_discriminator = member.split("#")
for ban_entry in banned_users:
user = ban_entry.user
if (user.name, user.discriminator) == (member_name, member_discriminator):
await ctx.guild.unban(user)
await ctx.send(f'Unbanned {user.mention}')
return
#The below code runs the bot.
client.run(token)
我希望这会有所帮助,祝你的机器人好运!
推荐阅读
- composer-php - 我可以清理/删除本地 Composer 目录吗?
- javascript - Typescript 或 babel 插件将 ES6 类转换为使用作用域变量作为私有属性模式的函数
- java - Java:在没有 Passay 和 VT-Password 库的情况下验证键盘序列规则
- symfony - 在 Symfony 中的 beforeSend 函数中获取 rootDir 以拦截 Sentry 事件
- php - 带有变量的 Laravel 视图,缺少路由所需的参数
- asp.net-core - 如何在 ASP.NET Core 3.1 中创建动态路由?
- nginx - Nginx 将子域 foo 重定向到 www.foo 到 www.www.foo 等
- python - Visual Studio 代码 - 未使用的导入警告 - Python
- mysql - 如何从 Sequelize 中的包含中引用父模型列?
- c# - ASP.net MVC - 我迷失在从数据库下载文件