通过discord-py上的命令author将dm发送给用户



所以,我从来没有使用过discord.py,这是我第一次使用它,我很困惑,我想制作一个命令,这样它就会在DM上发送帮助消息,但它不起作用,除非你提到自己。

import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '$')
@client.event
async def on_ready():
print(f'We have logged in as {client.user}')
@client.command()
async def help(ctx, user:discord.Member, *, message=None):
message = 'test'
embed = discord.Embed(title=message)
await user.send(embed=embed)

所以,如果你做$help,它不会起任何作用,但如果你做了$help@John Doe#0001,它会DM John Doe或你提到的任何人。如果这听起来很愚蠢,我很抱歉。。

如果你想让机器人向你(调用命令的人(发送消息,你不应该传递用户参数。但是,如果您仍然想提及某人以便向他们发送帮助消息,则可以在函数本身中设置user=None,这是一个可选参数。

请注意,在我提供的代码片段中,我使message不是一个参数,而是函数中已经存在的内容。请查看下面的修订代码。

@client.command()
async def help(ctx, user:discord.Member=None):
if user == None: # if no user is passed..
user = ctx.author # ..make the command author the user
message = "test"
embed = discord.Embed(title=message)
await user.send(embed=embed)

有用的问题

  • 如何向消息作者发送私人消息?-所以
  • 如何DM命令?-所以
  • ctx-Discord.py文档

您的意思是向使用该命令但没有提及用户的人发送帮助消息吗?

方法如下:

import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '$')
@client.event
async def on_ready():
print(f'We have logged in as {client.user}')
@client.command()
async def help(ctx):
message = 'test'
embed = discord.Embed(title=message)
await ctx.author.send(embed=embed)

使用ctx.author.send向使用命令的用户发送消息

如果您想在命令的通道以及dms添加await ctx.channel.send(embed=embed)中发送帮助提示,以下是要在dms中发送的帮助gui的代码

@client.command()
async def help(ctx):
embed = discord.Embed(color=0xFFFFFF)
embed.set_author(name='HELP MESSAGE' icon_url=ctx.author.avatar_url)
embed.add_field(name="help" , value='sends the help message in dms')
await ctx.author.send(embed=embed)

你可以添加你的自定义嵌入,而不是上面代码中的嵌入。。希望你能理解。

@client.command()
async def help(ctx, user:discord.Member, *, message=None):
embed = discord.Embed(title=f"{message}", description=f"Sent by {ctx.author}")
await user.send(embed=embed)

我会试试这样的。

最新更新