【问题标题】:Bot discord with python机器人与 python 不和谐
【发布时间】:2021-03-25 16:22:34
【问题描述】:

我该如何做到这一点,以便当不是管理员并运行“cls”命令的人给他一条消息,例如你不是管理员,对不起

Python 代码

@bot.command()
@commands.has_permissions(manage_messages=True)
async def cls(ctx, Quantity= 10000):
    if manage_messages == False:
        await ctx.send('You are not an administrator, you cannot run this command.')
    await ctx.channel.purge(limit = Quantity)

帮帮忙,我不知道该怎么做。

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    这就是我将如何检查消息的作者在您的情况下是否具有管理员权限的方法。

    if ctx.message.author.guild_permissions.administrator:
        await ctx.channel.purge(limit=Quantity)
        await ctx.send('Deletion success! Deleted ' + Quantity + ' messages.')
    else:
        await ctx.send('You are not an administrator, you cannot run this command.')
    

    【讨论】:

    • 谢谢,您的回答对我帮助很大,感谢您花时间回答这个问题。
    • 没问题!我刚刚开始学习 Discord.py,还有什么比与他人一起学习更好的学习方式!
    【解决方案2】:
    import discord
    import asyncio
    from discord.ext.commands import Bot
    
    client = Bot(description="My Cool Bot", command_prefix="!", pm_help = False, )
    @client.event
    async def on_ready():
      print("Bot is ready!")
      return await client.change_presence(game=discord.Game(name='My bot'))
    
    @client.command(pass_context = True)
    async def whoami(ctx):
        if ctx.message.author.server_permissions.administrator:
            msg = "You're an admin {0.author.mention}".format(ctx.message)  
            await client.send_message(ctx.message.channel, msg)
        else:
            msg = "You're an average joe {0.author.mention}".format(ctx.message)  
            await client.send_message(ctx.message.channel, msg)
    client.run('Your_Bot_Token')
    

    【讨论】:

    • 谢谢,您的回答对我帮助很大,感谢您花时间回答这个问题。
    猜你喜欢
    • 2021-10-30
    • 2021-05-27
    • 2021-07-15
    • 2017-08-12
    • 2021-03-29
    • 2021-07-11
    • 2018-08-13
    • 1970-01-01
    • 2021-02-28
    相关资源
    最近更新 更多