Discord.py - 嗨命令需要修复

时间:2021-04-15 12:40:53

标签: python discord.py

我想制作一个机器人,当有人输入 $hi @(someone) 时,它会回复 hi @(命令中提到的成员)。

代码:

import discord
import os
from discord.ext import commands
    
client = discord.Client()
   
bot = commands.Bot(command_prefix='$')
    
@bot.command()
async def hi(ctx, *,member: discord.Member):
    await ctx.send(member)
    bot.add_command(member, 'hi')
    
client.run(os.getenv("token"))

1 个答案:

答案 0 :(得分:0)

要提及成员,您可以使用不同的功能。

你的函数/命令现在对我不起作用或者没有给出你所期望的。

尝试以下操作:

@bot.command()
async def hi(ctx, *, member : discord.Member = None):
    if member is None: # Optional
        await ctx.send("You did not mention a member") # Optional
    else:
        await ctx.send("Hi, {}".format(member.mention)) # We mention a member and send "Hi"
  • 这里我们使用 format(member.mention) 来提及用户,并使用 {} 作为我们要填充的值 (member.mention)。
  • 如果您不想要 Optional 部分,只需将其删除即可。

或者第二种方法:

@bot.command()
async def hi(ctx, *, member : discord.Member = None):
    if member is None: # Optional
        await ctx.send("You did not mention a member") # Optional
    else:
        await ctx.send(f"Hi, {member.mention}!") # Use f-strings
  • 这里我们使用了 f 字符串,以使其“更”易于编写/编码。它的工作原理与上述方法类似,但现在我们可以将一个值插入到您的方括号 ({member.mention}) 中,而不必将其分开。
  • 如果您不想要 Optional 部分,只需将其删除即可。

如果您不想在两个命令中都包含“可选”部分,您还可以删除 = None

中的 member : discord.Member