[英]In discord.py, I have a clear/purge messages command. i want it to display a certain message when that command is executed
I have set the command permissions to be manage_messages.我已将命令权限设置为 manage_messages。 meaning, a person with manage messages permissions can only execute that command.
意思是,具有管理消息权限的人只能执行该命令。 but when a person without that permission uses it, i want it to display a custom message, like 'You cannot do that.'
但是当没有该权限的人使用它时,我希望它显示一条自定义消息,例如“你不能那样做”。 or something.
或者其他的东西。
here is my whole code.这是我的整个代码。
import os
import discord
from discord.ext import commands
import random
bot = commands.Bot(command_prefix = '-')
my_token = os.environ['Token']
@bot.event
async def on_ready():
print('Bot is Ready')
#errors
@bot.event
async def on_command_error(ctx, error):
if isinstance(error,commands.MissingRequiredArgument):
await ctx.send('Please enter all the required Arguments')
#help cmd
bot.remove_command('help')
@bot.group(invoke_without_command=True)
async def help(ctx):
em1 = discord.Embed(
title = 'Help',
description = 'Commands to get you started!'
)
em1.add_field(name = '8ball', value = 'random answers')
await ctx.send(embed = em1)
#ping latency....
@bot.command()
async def ping(ctx):
await ctx.send(f'Pong {round(bot.latency * 1000)}ms')
#8ball
@bot.command(aliases=['8ball', '8b'])
async def _8ball(ctx, *, question,):
responses = ['It is Certain.',
'It is decidedly so.',
'Without a doubt.',
'Yes definitely.',
'You may rely on it.',
'As I see it, yes.',
'Most likely.',
'Outlook good.',
'Yes.',
'Signs point to yes.',
'Reply hazy, try again.',
'Ask again later.',
'Better not tell you now.',
'Cannot predict now.']
await ctx.send(f'Question: {question} Answer: {random.choice(responses)}')
#clear
@bot.command()
@commands.has_permissions(manage_messages = True)
async def clear(ctx, amount=0):
await ctx.channel.purge(limit=amount)
keep_alive.keep_alive()
bot.run(my_token)```
@commands.has_permissions
raises commands.MissingPermissions
error if user has no permission to use.如果用户没有使用权限,
@commands.has_permissions
会引发commands.MissingPermissions
错误。
async def on_command_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send('Please enter all the required Arguments')
elif isinstance(error, commands.MissingPermissions):
await ctx.send("You are not allowed to do that.")
You can use permissions_in
method like that:您可以像这样使用
permissions_in
方法:
@bot.command()
async def clear(ctx, amount=0):
if ctx.author.permissions_in(ctx.channel).manage_messages:
await ctx.channel.purge(limit=amount)
else:
await ctx.send("You don't have permissions to do that")
This:这个:
ctx.author.permissions_in(ctx.channel).manage_messages
will check wether the author of the message have permissions to manage messages in the channel that the message was sent in, if the author have the permission it will return True
otherwise it will return False
.将检查消息的作者是否有权限管理发送消息的频道中的消息,如果作者有权限则返回
True
否则返回False
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.