【问题标题】:How can I have my Discord C# respond to mentions?如何让我的 Discord C# 响应提及?
【发布时间】:2018-05-14 11:45:40
【问题描述】:

我正在开发一个 C# discord 机器人,我已经成功地让机器人响应以“c!”开头的短语并使用正确的命令进行响应,但是我希望机器人回复 GIF如果提到了机器人。如果有人可以帮助解释为什么这不起作用以及如何解决它会很好。这是我现在的代码:

    private async Task HandleCommandAsync(SocketMessage arg)
    {
        var message = arg as SocketUserMessage;

        if (message is null || message.Author.IsBot) return;

        int argPos = 0;

        if (message.HasStringPrefix("", ref argPos))
        {
            var context = new SocketCommandContext(_client, message);

            var result = await _commands.ExecuteAsync(context, argPos, _services);

            if (!result.IsSuccess)
                Console.WriteLine(result.ErrorReason);
        }

        if (message.HasMentionPrefix(_client.CurrentUser, ref argPos))
        {
            var embed = new EmbedBuilder();
            embed.WithImageUrl("https://cdn.discordapp.com/attachments/138522037181349888/438774275546152960/Ping_Discordapp_GIF-downsized_large.gif");

            await ReplyAsync("", false, embed.Build());
        }

【问题讨论】:

    标签: c# discord.net


    【解决方案1】:

    我同意 Solarcloud 关于分离模块的说法,但这就是您必须将代码修改为:

    private async Task HandleCommandAsync(SocketMessage arg)
    {
        var message = arg as SocketUserMessage;
    
        if (message is null || message.Author.IsBot) return;
    
        int argPos = 0;
    
        if (message.HasStringPrefix("", ref argPos))
        {
            var context = new SocketCommandContext(_client, message);
    
            var result = await _commands.ExecuteAsync(context, argPos, _services);
    
            if (!result.IsSuccess)
                Console.WriteLine(result.ErrorReason);
        }
    
        if (message.content.contains(_client.CurrentUser.Mention.Replace("!", "")))
        {
            var embed = new EmbedBuilder();
            embed.WithImageUrl("https://cdn.discordapp.com/attachments/138522037181349888/438774275546152960/Ping_Discordapp_GIF-downsized_large.gif");
    
            await ReplyAsync("", false, embed.Build());
        }
    

    替换只是为了以防你的机器人的 ID 返回@<!123456789>,摆脱它可以正常工作。

    【讨论】:

    • 嗨,对我来说,当机器人尝试回复时,这会返回 NullRefferenceException,我将如何解决这个问题?
    【解决方案2】:

    最好创建一个 Modules 文件夹并将您的命令添加到单独的 .cs 文件中。为此,您需要在启动 _client 之后调用 LoginAsync 之前添加这行代码。

    _client.MessageReceived += HandleCommandAsync;
    await _commands.AddModulesAsync(Assembly.GetEntryAssembly());
    

    然后,创建一个单独的新类。您的命令可以接受 SocketGuildUser 作为参数。在聊天中输入 !help @name 来调用命令:

    public class Help : ModuleBase<SocketCommandContext>
    {
        [Command("help")]
        public async Task HelpAsync(SocketGuildUser user)
        {
            await ReplyAsync($"{user.Mention} needs a lot of help!");
        }
    }
    

    您的 HandleCommandAsync 应如下所示:

        private async Task HandleCommandAsync(SocketMessage arg)
        {
            var message = arg as SocketUserMessage;
    
            if (message == null || message.Author.IsBot) return;            
    
            int argPos = 0;
            if (message.HasStringPrefix("!", ref argPos) ||
                message.HasMentionPrefix(_client.CurrentUser, ref argPos))
            {
                var context = new SocketCommandContext(_client, message);
    
                var result = await _commands.ExecuteAsync(context, argPos, _services);
    
                if (!result.IsSuccess)
                {
                    Console.WriteLine(result.ErrorReason);
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-19
      • 2021-07-04
      • 2019-12-21
      • 2020-04-18
      • 2021-05-16
      • 2021-04-09
      • 2021-10-22
      • 2021-11-30
      相关资源
      最近更新 更多