Discord bot c# 获取用户信息(无参数)并发回消息

Discord bot c# get userinfo (without parameter)and send back message

您好,我正在用 C# 编写 Discord 机器人,我正在尝试获取使用该命令的人的用户信息(最好是用户名) 所以例如: 用户输入命令 - ?response (用户名是锤头) bot 读取用户名并输出:消息中的“HammerHead”以及其他内容

我要使用参数还是可以避免我基本上不希望用户输入他们的名字我希望机器人自动执行此操作。

    [Command("response")]
  
    public async Task Response(CommandContext ctx, string first, String last, string email, string 
    affiliation)
    {

       
       //would something like this work I don't really know
        var username = Context.user;


        await ctx.Channel.SendMessageAsync(first + " " + last + " " + email + " " + affiliation+" 
        "+user ).ConfigureAwait(false);


        }

因为你没有指定你使用的是哪个库,我会告诉你两个。

DSharpPlus:

您的 CommandContext 有 2 个属性,名为 UserMember。这两个都是调用你命令的用户,由于参数名为ctx,你只需要使用ctx.Userctx.Member。区别在于用户类型。 User 给你 DiscordUser 而 Member 给你 DiscordMember。

文档:https://dsharpplus.emzi0767.com/api/DSharpPlus.CommandsNext.CommandContext.html

Discord.Net:

DNet CommandContext 也有一个用户 属性。它会给你 IUser 类型。使用 ctx.User.

调用它

文档:https://discord.foxbot.me/docs/api/Discord.Commands.CommandContext.html#Discord_Commands_CommandContext_User

Tldr;

不管你有什么想法。只需要输入 ctx 而不是 Context,因为 ctx 是您的 CommandContext 参数的名称

旁注: 您可能想研究使用字符串插值。 微软文档:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated