当用户未指定昵称时调用机器人消息

时间:2018-04-20 12:29:01

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

我希望我的机器人写

  

错误,您没有指定昵称。

我的代码:

if message.content.startswith(prefix + 'kill'):
    name = message.mentions[0]
    randomgif = random.choice(["https://i.imgur.com/Pk6lOwI.gif","https://i.imgur.com/IRoF2pJ.gif","https://i.imgur.com/dV2N6KJ.gif","https://i.imgur.com/xyhP7Rz.gif","https://i.imgur.com/CzpHqFX.gif","https://i.imgur.com/iBsrA1c.gif","https://i.imgur.com/4jHevGp.gif","https://i.imgur.com/Oy2XGuL.gif","https://i.imgur.com/1uQwyli.gif","https://i.imgur.com/uCUf0V5.gif","https://i.imgur.com/xfubfyv.gif","https://i.imgur.com/8th0trO.gif","https://i.imgur.com/2jOXQcP.gif","https://i.imgur.com/2kCGfie.gif","https://i.imgur.com/TPV6Upz.gif","https://i.imgur.com/Bti43x8.gif"])
    embed=discord.Embed(color=0x008C8C)
    embed.set_author(name= message.author.display_name + " убил " + name.display_name, icon_url='https://pre00.deviantart.net/7e08/th/pre/f/2017/125/2/2/felix_argyle_by_aramisdraws-db8847i.png')
    embed.set_image(url= randomgif)
    await client.send_message(message.channel, embed=embed)

1 个答案:

答案 0 :(得分:0)

Member.nick是成员的服务器特定昵称。如果没有这样的名字,它将是None。

import discord
from discord.ext import commands
import random

bot = commands.Bot(command_prefix=prefix)

@bot.command(pass_context=True)
async def kill(ctx, member: discord.Member):
    if member.nick is None:
        await bot.say("Error!  You did not specify a nickname")
        return
    randomgif = random.choice(["https://i.imgur.com/Pk6lOwI.gif","https://i.imgur.com/IRoF2pJ.gif","https://i.imgur.com/dV2N6KJ.gif","https://i.imgur.com/xyhP7Rz.gif","https://i.imgur.com/CzpHqFX.gif","https://i.imgur.com/iBsrA1c.gif","https://i.imgur.com/4jHevGp.gif","https://i.imgur.com/Oy2XGuL.gif","https://i.imgur.com/1uQwyli.gif","https://i.imgur.com/uCUf0V5.gif","https://i.imgur.com/xfubfyv.gif","https://i.imgur.com/8th0trO.gif","https://i.imgur.com/2jOXQcP.gif","https://i.imgur.com/2kCGfie.gif","https://i.imgur.com/TPV6Upz.gif","https://i.imgur.com/Bti43x8.gif"])
    embed=discord.Embed(color=0x008C8C)
    embed.set_author(name= ctx.message.author.display_name + " убил " + member.display_name, icon_url='https://pre00.deviantart.net/7e08/th/pre/f/2017/125/2/2/felix_argyle_by_aramisdraws-db8847i.png')
    embed.set_image(url= randomgif)
    await bot.say(embed=embed)

如果您正在实施命令,那么您应该使用discord.ext.commands模块,这就是它的用途。