【问题标题】:How do I get a discord bot send a DM to someone I ping?如何让 Discord 机器人向我 ping 的人发送 DM?
【发布时间】:2021-06-13 04:30:51
【问题描述】:

我是一个相当新的编码,并认为我会尝试制作一个不和谐的机器人。到目前为止,它很容易理解,我已经开始尝试为我的机器人发出命令。但我不知道如何让我的机器人给我 ping ex 的人发 DM。 !warn @person(原因)。我已经尝试查找它,但无法找到方法。

[Command("warn")]
[RequireUserPermission(GuildPermission.KickMembers, ErrorMessage = "You don't have the persmission ''warn_member''!")]
public async Task WarnMember(IGuildUser user = null, [Remainder] string reason = null)
{
    if (user == null)
    {
        await ReplyAsync("Please Specify A User"); return;
    }
    if (reason == null) reason = "Not Specified";

这是我试图发送 DM 的地方,但它会将其发送给运行命令的人,而不是我 ping 的人

await Context.User.SendMessageAsync("You have been warned for " + reason);

var EmbedBuilder = new EmbedBuilder()
    .WithDescription($":white_check_mark: {user.Mention} was warned\n**Reason **{reason}")
    .WithFooter(footer =>
    {
         footer
         .WithText("User Warn Log");
               
    });
    Embed embed = EmbedBuilder.Build();
    await ReplyAsync(embed: embed);
}

【问题讨论】:

    标签: c# discord discord.net


    【解决方案1】:

    Context.User 总是指正在执行命令的用户。要向命令中提到的人发送消息,您需要在您的用户参数上调用 SendMessageAsync() 函数。

    await user.SendMessageAsync(...)
    

    请记住,用户可以为您的机器人所在的服务器禁用直接消息,这可能会导致异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-23
      • 2022-08-11
      • 2020-09-14
      • 2021-03-03
      • 2022-01-23
      • 2020-03-05
      • 2021-05-11
      • 2022-10-04
      相关资源
      最近更新 更多