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

Discord.Net异步方法等待消息报错:GetInteractivityModule未定义

解决Discord.Net中等待用户回复的问题

原因说明

旧版本Discord.Net里的GetInteractivityModule方法已被移除,目前Interactivity功能作为独立NuGet包提供,需要单独安装并通过InteractivityService实现等待用户回复的逻辑。

解决步骤

  1. 安装Interactivity包
    在项目中安装Discord.Net.Interactivity NuGet包,可通过NuGet包管理器或命令行执行:

    Install-Package Discord.Net.Interactivity
    

    或使用.NET CLI:

    dotnet add package Discord.Net.Interactivity
    
  2. 初始化InteractivityService
    机器人启动时需初始化InteractivityService,支持两种方式:

    • 手动实例化:
      // 假设已存在DiscordSocketClient实例client
      var interactivityConfig = new InteractivityConfig
      {
          DefaultTimeout = TimeSpan.FromSeconds(30) // 设置默认等待超时时间
      };
      var interactivity = new InteractivityService(client, interactivityConfig);
      
    • 依赖注入(若用Host构建项目):
      builder.Services.AddSingleton<InteractivityService>(sp =>
      {
          var client = sp.GetRequiredService<DiscordSocketClient>();
          var config = new InteractivityConfig { DefaultTimeout = TimeSpan.FromSeconds(30) };
          return new InteractivityService(client, config);
      });
      
  3. 在SwitcherAsync中等待用户回复
    在你的SwitcherAsync方法里,用InteractivityService的WaitForMessageAsync方法监听指定用户的回复:

    public async Task SwitcherAsync(SocketMessage message)
    {
        var user = message.Author as SocketGuildUser;
        if (message.Content.Equals("!wait", StringComparison.OrdinalIgnoreCase))
        {
            await message.Channel.SendMessageAsync("请回复你的内容:");
            
            // 过滤仅接收该用户的消息,设置30秒超时
            var reply = await interactivity.WaitForMessageAsync(
                msg => msg.Author.Id == user.Id,
                TimeSpan.FromSeconds(30)
            );
            
            if (reply.IsSuccess)
            {
                await message.Channel.SendMessageAsync($"你回复的内容是:{reply.Value.Content}");
            }
            else
            {
                await message.Channel.SendMessageAsync("等待超时,未收到你的回复。");
            }
        }
    }
    

注意事项

  • 确保Discord.Net主包与Discord.Net.Interactivity包版本匹配,避免兼容问题。
  • 可根据需求调整InteractivityConfig的参数,比如修改超时时间、是否允许私信回复等。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 00:27:09