public async Task RestoreApplication(SocketGuild guild, GuildDB database) { // check if this guild has applications ApplicationTB application = GetApplicationEntry(guild, database); if (application == null) { logger.Log(new LogMessage(LogSeverity.Info, "State", $"'{guild.Name}' currently has no application.")); return; } // make sure that there is not an already active notifier // (This could happen, because GuildAvailable gets called when Betty loses connection with discord) CancellationTokenSource t = GetApplicationToken(guild); if (t != null) { if (!t.IsCancellationRequested) { // if it's still active, there is no need to restore the application logger.Log(new LogMessage(LogSeverity.Warning, "State", $"Found an active notifier for application.")); return; } else { // if it's not active, then there is something wrong with the code, but then a new notifier can be created still logger.Log(new LogMessage(LogSeverity.Error, "State", $"Found a notifier, but cancellation was requested. This should never happen!!")); } } // create notifier SocketTextChannel channel = GetApplicationChannel(guild, database, application); IInviteMetadata invite = await GetApplicationInvite(guild, database, application, channel); var token = notifier.CreateWaiterTask(guild, channel, messages: DateTimeMethods.BuildMessageList(constants.ApplicationNotifications, application.Deadline, "Application selection"), action: async(db) => { await ExpireAppLink(invite); }); SetApplicationToken(guild, database, token); }
public bool Plan(SocketGuild guild, GuildDB database, string name, DateTime date, SocketTextChannel channel = null, bool doNotifications = true, TimeSpan[] notifications = null) { // generate database data var ev = StoreEventInDatabase(guild, database, name, date, channel, doNotifications, notifications); // create notifier for this event var token = notifier.CreateWaiterTask(guild, null, messages: DateTimeMethods.BuildMessageList(constants.EventNotifications, ev.Event.Date, ev.Event.Name), action: (db) => { Cancel(ev.Event, db); }); // store cancellation token if (!notifiercollection.TryAdd(ev.Event.EventId, token)) { // log failure and report back logger.Log(new LogMessage(LogSeverity.Error, "Agenda", "Attempted to store cancellation token for event, but failed")); return(false); } return(true); }
public async Task <IInviteMetadata> StartApplication(SocketGuild guild, GuildDB database, DateTime deadline, GuildTB dbentry = null) { if (dbentry == null) { dbentry = GetGuildEntry(guild, database); } // create a channel var channel = await CreateApplicationChannel(guild, dbentry); if (channel == null) { return(null); } // create an invite var invite = await CreateApplicationInvite(guild, channel, dbentry); if (invite == null) { return(null); } // save to database if (!SaveApplicationToDatabase(dbentry, deadline, channel, invite, database)) { return(null); } // set a notifier var token = notifier.CreateWaiterTask(guild, GetApplicationChannel(guild, database), messages: DateTimeMethods.BuildMessageList(constants.ApplicationNotifications, deadline, "Application selection"), action: async(db) => { await ExpireAppLink(invite); }); SetApplicationToken(guild, database, token, dbentry); return(invite); }