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

时间:2018-11-20 19:47:19

标签: python python-3.x bots 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)

对于我的不和谐机器人,我想做到这一点,以便所有人都能使用!test,并且该机器人会回答“此消息是测试”。此外,我希望!admin仅由所有者角色执行,并且该漫游器只会以“只能以所有者身份执行此命令”作为响应,否则它将以“对不起,您没有使用此命令的权限”进行答复命令”。我该如何实现?

1 个答案:

答案 0 :(得分:0)

这是检查邮件是否来自服务器所有者(不需要角色)的方法:

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 documentation中找到。