【问题标题】:Discord C# Bot - Delete the latest messageDiscord C# Bot - 删除最新消息
【发布时间】:2017-10-25 22:33:15
【问题描述】:

我正在为我的不和谐机器人使用流氓异常包。

当用户通过命令调用机器人时,我希望机器人在执行命令之前删除他的消息。

所以在我的“MessageReceived”事件中,到目前为止我有这个代码:

private async Task MessageReceived(SocketMessage s)
        {
            var msg = s as SocketUserMessage; // the input

            if (msg == null || !msg.Content.StartsWith("!") || msg.Author.IsBot) // return if null, msg is no command, msg is written by the bot
                return;

            if (msg.Channel is SocketGuildChannel guildChannel) // just delete the command if the msg is written in the guild channel, not a private message channel
                await ??? // Delete the command
}

那么有人知道,那里写什么=?

【问题讨论】:

    标签: c# discord


    【解决方案1】:

    我可以看到您正在为您的机器人使用 Discord.NET API。
    所以从这个文档here.

    查看SocketMessage 属性中的Method 列表。 (顺便看一下网页的右侧,应该可以看到一个让您轻松导航的栏)

    如果您想知道我们为什么要查看 SocketMessage,那是因为您的代理将在用户发布消息时运行,并且该消息是您的 SocketMessage,因此这就是您要删除的内容。

    您应该能够看到一个名为:DeleteAsync 的方法。这就是你想要的。
    (截图如下)

    对于参数,您可以看到它是RequestOption 数据类型,默认情况下它已为您设置null。您可以更改它,但我强烈建议使用默认值。

    另外需要注意的是,如果机器人没有管理消息的权限,它将什么都不做(并返回异常)。

    【讨论】:

      【解决方案2】:

      示例:

      (抱歉,有些代码字符串是葡萄牙语...这是一个投票系统)

              [Command("Votar")]
              [Summary("Abro uma votação, com opções de Sim e Não.")]
              [RequireBotPermission(ChannelPermission.AddReactions)]
              public async Task NovoVoto([Remainder] string secondPart)
              {
                  Log.CMDLog(Context.User.Username, "Votar", Context.Guild.Name);
      
                  if (secondPart.Length >= 200)
                  {
                      await Context.Channel.SendMessageAsync("Perdão, porém seu voto não deve ter mais de 200 caracteres");
                      return;
                  }
      
                  var embed = new EmbedBuilder();
                  embed.WithColor(new Color(126, 211, 33));
                  embed.WithTitle("VOTE AQUI!");
                  embed.WithDescription(secondPart);
                  embed.WithFooter($"Votação criada por: {Context.User.Username}");
      
                  RestUserMessage msg = await Context.Channel.SendMessageAsync("", embed: embed);
                  await msg.AddReactionAsync(new Emoji("✅"));
                  await msg.AddReactionAsync(new Emoji("❌"));
      
                  //Delete the command message from the user
                  await Context.Message.DeleteAsync();
              }
      

      【讨论】:

        猜你喜欢
        • 2021-01-28
        • 2020-12-03
        • 2020-08-25
        • 2018-07-24
        • 1970-01-01
        • 2021-09-14
        • 2021-10-18
        • 2020-10-01
        • 2018-11-07
        相关资源
        最近更新 更多