You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现Discord机器人定时器命令结束后@提及触发用户?

解决Discord定时器命令完成时@触发用户的问题

嘿,我懂你的需求啦!你现在的代码里直接把user对象拼接到消息里,Discord没法自动识别成可触发通知的@提及,咱们只需要用Discord.NET提供的Mention属性就能搞定。

修改后的完整代码

[Command("timer")]
[Summary("**%timer** + time in seconds and then it will tell you when it's done!")]
private async Task test(string input) {
    User user = Context.User;
    string name = Context.User.Username;
    
    // 额外加个输入验证,避免非数字输入导致程序报错
    if (!int.TryParse(input, out int time))
    {
        await Context.Channel.SendMessageAsync("请输入有效的数字作为秒数哦!");
        return;
    }
    
    time *= 1000;
    await Context.Channel.SendMessageAsync($":stopwatch: **{name}** has put a timer on **{input}** seconds! :stopwatch:");
    await Task.Delay(time);
    
    // 这里用user.Mention生成Discord能识别的@格式
    await Context.Channel.SendMessageAsync($":stopwatch: **{user.Mention}'s** timer on **{input}** seconds is done! :stopwatch:");
}

关键说明

  • user.Mention会自动生成Discord专属的@用户格式:<@用户ID>,发送这条消息后,触发命令的用户会收到@通知,消息里的用户名也是可点击跳转的。
  • 我额外加了int.TryParse的输入验证逻辑,避免用户输入非数字内容时程序直接崩溃,让命令的健壮性更好一点。

内容的提问来源于stack exchange,提问作者Xilefen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:10:56