public async Task RegisterChannel(CommandContext ctx, DiscordChannel channel = null) { // get member's voice state var vstat = ctx.Member?.VoiceState; if (vstat?.Channel == null) { // they did not specify a channel and are not in one await ctx.RespondAsync("You are not in a voice channel."); return; } GuildHandler handler = KoekoeController.GetGuildHandler(ctx.Client, vstat.Channel.Guild, true); if (handler != null) { handler.AddChannel(vstat.Channel); //Could throw access violation because we run the handlers async, this needs fixing } if (!handler.IsRunning) { handler.Execute(); //Will run async } await ctx.RespondAsync($"Registered to `{vstat.Channel.Name}`"); }
//Hooked up in program.cs public static void StartupGuildHandler(DiscordClient sender, GuildCreateEventArgs e) { // let's log the name of the guild that was just // sent to our client //sender.Logger.LogInformation(Program.BotEventId, $"Guild available: {e.Guild.Name}"); //Create or get the handler for this guild GuildHandler handler = KoekoeController.GetGuildHandler(sender, e.Guild); //Read our saved data string guilddata_path = Path.Combine(Environment.CurrentDirectory, "data", $"guilddata_{e.Guild.Id}.json"); if (File.Exists(guilddata_path)) { string json = File.ReadAllText(guilddata_path).ToString(); var dataObj = JsonConvert.DeserializeObject <SavedGuildData>(json); //Restore registered channels if (dataObj.channelIds != null) { foreach (ulong channelid in dataObj.channelIds) { DiscordChannel channel = e.Guild.GetChannel(channelid); if (channel != null) { handler.AddChannel(channel, false); } } } //Restore alarms if (dataObj.alarms != null) { foreach (AlarmData alarm in dataObj.alarms) { if (alarm != null) { handler.AddAlarm(alarm.AlarmDate, alarm.AlarmName, alarm.userId, false); } } } } if (!handler.IsRunning) //Run the handler loop if it's not already started { handler.Execute(); } }