所以我写了一个用于批量删除消息的代码,但我只希望管理员能够使用这个命令。
import discord
import asyncio
from discord.ext.commands import Bot
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as', self.user)
await client.change_presence(activity=discord.Game('NLRP with MaximaVoid'))
async def on_message(self, message):
# bulk deletion command
if message.content.startswith('!ldlmsg'):
await message.channel.purge()
if message.content.startswith('!bdelmsg50'):
delnum = 50
await message.channel.purge(limit = delnum)
if message.content.startswith('!bdelmsg10'):
delnum = 10
await message.channel.purge(limit = delnum)
答案 0 :(得分:1)
我在 on_message 中的做法:
if message.author.guild_permissions.administrator == True:
如果 message.author 是公会/服务器上的管理员,则返回 true,否则返回 false。
答案 1 :(得分:0)
我不确定您是指名为 Admin 的角色中的“admin”,还是机器人所有者。
如果是bot所有者,可以添加装饰器:
@commands.is_owner()
如果它是一个名为 Admin 的角色,你可以使用装饰器:
@commands.has_role("Admin")
执行代码的正确方法是将其分离到不同的函数中,而不是检查 on_message
中的所有命令。您应该阅读 discord.py 文档:
https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html?highlight=cogs
答案 2 :(得分:0)
您可以使用以下装饰器:
@commands.is_owner() # If you only want the owner to execute this command
如果有名为 Admin
或 Administrator
或类似名称的角色:
@commands.has_role('Admin') # Anyone who executes the command must have the 'Admin' role to continue
如果您希望具有任何指定角色的人能够执行命令:
@commands.has_any_role('Admin', 'Administrator') # You can add as many roles as you want
最后,如果您只希望拥有“管理员”权限的人能够执行此命令:
@commands.has_permissions(administrator=True, ban_members=True) # You can add as many permissions as you would like