简体   繁体   English

在 discord.py 中,我有一个清除/清除消息命令。 我希望它在执行该命令时显示特定消息

[英]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.

相关问题 我在 discord.py 中有错误(清除命令) - I have a error in discord.py (purge command) 运行清除命令后机器人没有响应。 discord.py - Bot not responding after running the purge command. discord.py 我正在尝试为我的 discord.py 机器人设置清除/清除命令。 没有错误,但是当我运行命令时,什么也没有发生 - Im trying to setup a clear/purge command for my discord.py bot. There are no errors but when I run the command, nothing happens 我想发送带有嵌入禁止命令的消息(Discord.py) - I want to send message with embed at ban command (Discord.py) 我想使用 discord.py 发出命令,当有人对消息做出反应时触发 - I want to make a command using discord.py which triggers when someone react to the message 执行命令时如何获取发送的消息 Discord.py - How to get the sent message when a command is executed Discord.py Discord.py清除命令中的人员列表和删除的消息数 - Discord.py The list of people and the number of deleted messages in purge command 为什么当我编辑一条消息并尝试该命令但没有工作时,我的命令显示“这里没有编辑任何消息” discord.py - Why does my command shows that there is “No messages were edited here” when i edited a message and tried the command and didnt work discord.py 如何使用Discord.py创建清除命令 - How To Create A Purge Command With Discord.py Discord.py 清除命令未清除 - Discord.py Purge command not purging
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM