使Discord知道输入并使用它来提供输出python

时间:2018-08-05 15:06:34

标签: python discord discord.py

我在python 3.5中制作Discord机器人。我希望机器人知道我何时写“; info @example:1111”并提供输出:

  • 用户名是:
  • 用户ID为:
  • 用户在以下位置加入了...

我不明白这一点。

这是我的代码:

@client.event
async def on_message(message, user: discord.Member):    
    if message.content.upper().startswith(';INFO'):
        if "248783308341772288" in [role.id for role in message.author.roles]:
            await client.send_message(message.channel,"The users name is: {}".format(user.name))
            await client.send_message(message.channel,"The users ID is: {}".format(user.id))
            await client.send_message(message.channel,"The users status is: {}".format(user.status))
            await client.send_message(message.channel,"The users highest role is: {}".format(user.top_role))
            await client.send_message(message.channel,"The user joined at: {}".format(user.joined_at))
        else:
            print("No access!")

1 个答案:

答案 0 :(得分:0)

您正在尝试在on_message协程中编写命令。而是使用ext.commands。在下面,我定义了一个以用户为参数的命令INFO。我们使用has_role check来查看呼叫者是否具有正确的角色。如果没有,协程向他们发送消息时会出错。

from discord.ext import commands
import discord

bot = commands.Bot(command_prefix=';')

@bot.command(pass_context=True)
@commands.has_role("ROLE NAME")
async def INFO(ctx, user: discord.Member):
    channel = ctx.message.channel
    await bot.send_message(channel,"The users name is: {}".format(user.name))
    await bot.send_message(channel,"The users ID is: {}".format(user.id))
    await bot.send_message(channel,"The users status is: {}".format(user.status))
    await bot.send_message(channel,"The users highest role is: {}".format(user.top_role))
    await bot.send_message(channel,"The user joined at: {}".format(user.joined_at))

@INFO.error
async def info_error(error, ctx):
    if isinstance(error, commands.CheckFailure):
        await bot.send_message(ctx.message.channel, "Insufficent permission")