Discord.py机器人,根据特定角色执行响应



我的python脚本:

import discord
TOKEN = 'X'
client = discord.Client()
@client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
if message.content.startswith('!test'):
msg = "This message is a test".format(message)
await client.send_message(message.channel, msg)
if message.content.startswith('!admin'):
msg = "This command can be executed only as the owner".format(message)
await client.send_message(message.channel, msg)
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
client.run(TOKEN)

对于我的不和机器人,我想做到这一点!测试可以被每个人使用,机器人会回复"这条消息是一个测试"。另外,我想要!admin只能由所有者角色执行,机器人程序只会回复"此命令只能作为所有者执行",否则它会回复"对不起,您没有使用此命令的权限"。我怎样才能做到这一点?

这就是检查消息是否来自服务器所有者的方法,不需要任何角色:

if message.content.startswith('!admin'):
if (message.author == message.server.owner):
# do stuff
else:
msg = "This command can only be executed by the owner."
await client.send_message(message.channel, msg)

当然,如果这样做,首先需要确保私有(直接(消息不会到达该语句,因为它们没有"server"参数。可以这样做:

if (message.channel.type == discord.ChannelType.private):
return

所有这些都可以在discord.py文档中找到。

最新更新