0

我正在为 Discord 创建一个 Bot,它会在用户加入语音聊天时宣布。我已经完成了让所有用户进入语音聊天并将其存储在列表中的实现,然后我使用一个在列表的设置器更改时引发的事件。我的问题是,当我尝试进入不和谐的语音聊天时,我对 Console.WriteLine("testing") 的测试不起作用。这是一个问题,因为用户列表正在更改,但未引发事件以通知订阅者此更改。我知道这是 DiscordShardedClient 类中的一个事件 VoiceStateUpdated,但该路线更高级一些,我无法完成这项工作。

class ChannelWatcher
    {
        private List<DiscordUser> _activeUsers = new List<DiscordUser>();

        public List<DiscordUser> ActiveUsers
        {
            get { return _activeUsers; }
            set
            {
                _activeUsers = value;
                OnActiveUsersChanged();
            }
        }
        public DiscordGuild Guild { get; set; }
        VoiceNextExtension Vnext { get; set; }
        DiscordUser User { get; set; }
        public VoiceNextConnection Connection { get; set; }
        public IEnumerable<KeyValuePair<ulong, DiscordChannel>> ServerChannels { get; set; }
        public event EventHandler<List<DiscordUser>> ActiveUsersChanged;
        public ChannelWatcher(DiscordGuild guild, VoiceNextExtension voiceNext)
        {
            this.Guild = guild;
            this.Vnext = voiceNext;
            ServerChannels = this.Guild.Channels.Where(x => x.Value.Type == ChannelType.Voice);
        }
        public Task MonitorChannelAudience()
        {
            foreach (var channel in ServerChannels)
            {
                ActiveUsers.AddRange(channel.Value.Users.ToList());
            }
            return Task.CompletedTask;
        }
        protected virtual void OnActiveUsersChanged()
        {
            if (ActiveUsersChanged != null)
            {
                ActiveUsersChanged(this, ActiveUsers);
            }
        }        
    }

//Subscription to event

class ConfigModule:BaseCommandModule
    {
        [Command("Start-NA")]
        public async Task StartCommand(CommandContext ctx, DiscordChannel channel = null)
        {
            ChannelWatcher OverSeer = new(ctx.Guild, ctx.Client.GetVoiceNext());
            await OverSeer.MonitorChannelAudience();
            OverSeer.ActiveUsersChanged += OverSeer_ActiveUsersChanged;
           
        }
        //handler never called
        private void OverSeer_ActiveUsersChanged(object sender, List<DiscordUser> e)
        {
            Console.WriteLine("Testing...");
        }
    }
4

0 回答 0