Discord bot C#获取用户信息(不带参数)并发送回消息

时间:2020-06-27 13:11:32

标签: c# bots discord userinfo

嗨,我正在用C#做一个Discord机器人,我正在尝试获取使用该命令的人的用户信息(最好是用户名) 因此: 用户键入命令-响应 (用户名是Hammerhead) 机器人读取用户名并在消息中输出“ 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);


        }

1 个答案:

答案 0 :(得分:1)

由于您没有指定正在使用的库,因此我只告诉您两者。

DSharpPlus:

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

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

Discord.Net:

DNet CommandContext也具有User属性。它会给你IUser类型。使用ctx.User对其进行调用。

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

Tldr;

无论哪种方式,您都有想法。只需输入ctx而不是Context,因为ctx是您的CommandContext参数的名称

侧面说明: 您可能想研究使用字符串插值。 MS Docs:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated