【问题标题】:Discord.py bot that executes responses based on specific role [duplicate]根据特定角色执行响应的 Discord.py 机器人 [重复]
【发布时间】:2019-04-23 08:08:57
【问题描述】:

我的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)

对于我的 discord 机器人,我想这样做,以便每个人都可以使用 !test,并且机器人会以“此消息是测试”作为响应。另外,我希望 !admin 只能由所有者角色执行,机器人只会回复“此命令只能作为所有者执行”,否则它会回复“对不起,您没有使用此命令的权限”命令”。我怎样才能做到这一点?

【问题讨论】:

  • 对于所有者角色你了解多少?它的 id、它的名字等......
  • 让我改写一下,而不是使用所有者角色,我将如何使命令只有在用户具有角色“成员”时才能执行?

标签: python python-3.x bots discord.py


【解决方案1】:

这是检查消息是否来自服务器所有者的方法,无需角色:

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)

当然,如果您这样做,您首先需要确保私人(直接)消息不会到达此语句,因为它们没有“服务器”参数。可以这样做:

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

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

【讨论】:

  • 好吧,假设我希望 !admin 由角色“成员”使用,我将如何确保消息作者与角色“成员”相同。
猜你喜欢
  • 2019-04-20
  • 2021-11-28
  • 1970-01-01
  • 2019-08-26
  • 2021-08-18
  • 2021-11-03
  • 2020-10-16
  • 2021-10-06
  • 1970-01-01
相关资源
最近更新 更多