所以我写了一个批量消息删除的代码,但是我希望只有管理员能够使用这个命令。
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)
发布于 2021-01-13 15:50:22
我在on_message中使用的方法:
if message.author.guild_permissions.administrator == True:
如果message.author是公会/服务器上的管理员,则返回true,否则返回false。
发布于 2021-01-13 15:24:25
我不太确定你指的是" Admin“,还是指的是机器人的所有者。
如果是机器人所有者,可以添加装饰器:
@commands.is_owner()
如果是名为Admin的角色,则可以使用装饰器:
@commands.has_role("Admin")
做你的代码的正确方法应该是在不同的函数中分离它,而不是检查on_message
中的所有命令。您应该阅读discord.py文档:https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html?highlight=cogs
发布于 2021-01-13 20:28:21
您可以使用以下装饰器:
@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
最后,如果您只希望拥有'Administrator‘权限的用户能够执行此命令:
@commands.has_permissions(administrator=True, ban_members=True) # You can add as many permissions as you would like
https://stackoverflow.com/questions/65697173
复制相似问题