private void reschedule(ReminderObj obj) { obj.nextSchedule = obj.nextSchedule + new TimeSpan(0, obj.freq, 0); var ls = reminderlist; LinkedList <ReminderObj> prev = null; while (ls != null && ls.obj.nextSchedule < obj.nextSchedule) { prev = ls; ls = ls.next; } if (prev == null) { reminderlist = new LinkedList <ReminderObj>(obj, ls); } else { var wrappedobj = new LinkedList <ReminderObj>(obj, ls); prev.next = wrappedobj; } if (!running) { running = true; var z = new Timer((reminderlist.obj.nextSchedule - DateTime.Now).TotalMilliseconds); z.Elapsed += (s, e2) => sendMessage((Timer)s); z.Start(); } }
private void addfunc(ServerMessage arg1, String name, String channelname, String frequency, String msg) { var channel = server.getServer().TextChannels.Where(chn => chn.Name == channelname).FirstOrDefault(); if (reminderlist != null && reminderlist.Any(x => x.name == name)) { server.safeSendMessage(arg1.Channel, "error: name already exists"); return; } if (channel == null) { server.safeSendMessage(arg1.Channel, "error: no such channel"); return; } Int32 freq; if (!Int32.TryParse(frequency, out freq) || freq <= 0) { server.safeSendMessage(arg1.Channel, "error: frequency needs to be a whole number greater 0"); return; } ReminderObj k = new ReminderObj(); k.freq = freq; k.nextSchedule = DateTime.Now; k.name = name; k.channelid = channel.Id; k.msg = msg; emitMsg(k); reschedule(k); server.safeSendMessage(arg1.Channel, "successfully added " + name + " at an interval of " + k.freq + " minutes."); }
private void emitMsg(ReminderObj obj) { var chan = server.getServer().GetChannel(obj.channelid); if (chan == null) { return; } SocketTextChannel stc = chan as SocketTextChannel; if (stc != null) { return; } var msg = stc.CachedMessages.FirstOrDefault(); Console.WriteLine(msg.Author + " with " + msg.Content); if (msg != null && msg.Source == Discord.MessageSource.Bot) { return; } server.safeSendMessage(stc, obj.msg); }