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(); } }
public async Task SetAlarm(CommandContext ctx, string alarmname, [RemainingText, Description("Alarm time ex 4:20 or 15:34")] string datestring) { // 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; } string[] datestringComps = datestring.Split(':'); if (datestringComps.Length == 2) { int parsedHours, parsedMinutes; if (int.TryParse(datestringComps[0], out parsedHours) && int.TryParse(datestringComps[1], out parsedMinutes)) { if (parsedHours > 0 && parsedHours < 24 && parsedMinutes > 0 && parsedMinutes < 60) { int hourdiff = (parsedHours - DateTime.Now.Hour) % 24; if (hourdiff < 0) { hourdiff += 24; } int mindiff = (parsedMinutes - DateTime.Now.Minute) % 60; DateTime dt = DateTime.Now.AddHours(hourdiff).AddMinutes(mindiff).AddSeconds(-DateTime.Now.Second); GuildHandler handler = KoekoeController.GetGuildHandler(ctx.Client, vstat.Channel.Guild, true); handler.AddAlarm(dt, alarmname, ctx.User.Id); if (!handler.IsRunning) //If the handler isn't running for some reason start it TODO: remove these, or implement handler sleep { handler.Execute(); //Will run async } } } } await ctx.RespondAsync($"Registered alarm `{alarmname}` to `{ctx.User.Username}`"); }