private bool SaveApplicationToDatabase(GuildTB dbentry, DateTime deadline, ITextChannel channel, IInviteMetadata invite, GuildDB database) { // create entry ApplicationTB appdbentry = new ApplicationTB { Deadline = deadline, InviteID = invite.Id, Channel = channel.Id, Guild = dbentry, }; database.Applications.Add(appdbentry); try { // try to save to database database.SaveChanges(); return(true); } catch (Exception e) { // log failure logger.Log(new LogMessage(LogSeverity.Error, "State", $"Attempted to save application to database, but failed: {e.Message}\n{e.StackTrace}")); return(false); } }
public async Task StopApplication([Remainder] string input = null) { using (var database = new GuildDB()) { // log command execution CommandMethods.LogExecution(logger, "application stop", Context); // indicate that the bot is working on the command await Context.Channel.TriggerTypingAsync(); StringConverter language = statecollection.GetLanguage(Context.Guild, database); // make sure that the user has the right permissions if (!PermissionHelper.UserHasPermission(Context.User as SocketGuildUser, PermissionHelper.Admin, database)) { await Context.Channel.SendMessageAsync(language.GetString("command.nopermission")); return; } // make sure that applications are taking place ApplicationTB appentry = statecollection.GetApplicationEntry(Context.Guild, database); if (appentry == null) { await Context.Channel.SendMessageAsync(language.GetString("command.appstop.noapp")); return; } // stop the applications await statecollection.StopApplication(Context.Guild, database, appentry); await Context.Channel.SendMessageAsync(language.GetString("command.appstop.success")); } }
private bool GetApplicationEntry(SocketGuild guild, GuildDB database, ref ApplicationTB application) { // make sure that there are indeed applications going if (application == null) { application = GetApplicationEntry(guild, database); if (application == null) { return(false); } } return(true); }
private bool RemoveApplicationFromDatabase(ApplicationTB appentry, GuildDB database) { database.Applications.Remove(appentry); try { //try to apply removal to database database.SaveChanges(); return(true); } catch (Exception e) { //log failure logger.Log(new LogMessage(LogSeverity.Warning, "State", $"Attempted to remove application from database, but failed: {e.Message}\n{e.StackTrace}")); return(false); } }
private async Task <bool> DeleteApplicationChannel(SocketGuild guild, ApplicationTB appentry) { var channel = GetChannel(guild, appentry.Channel); try { // try to delete the channel await channel.DeleteAsync(); return(true); } catch (Exception e) { // log failure logger.Log(new LogMessage(LogSeverity.Warning, "State", $"Attempted to delete application channel for '{guild.Name}', but failed: {e.Message}\n{e.StackTrace}")); return(false); } }
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 async Task Status([Remainder] string input = null) { using (var database = new GuildDB()) { // log execution CommandMethods.LogExecution(logger, "get status", Context); // indicate that the command is being worked on await Context.Channel.TriggerTypingAsync(); GuildTB gtb = statecollection.GetGuildEntry(Context.Guild, database); StringConverter language = statecollection.GetLanguage(Context.Guild, database, gtb); // make sure that the user has the right permissions if (!PermissionHelper.UserHasPermission(Context.User as SocketGuildUser, PermissionHelper.Admin, database)) { await Context.Channel.SendMessageAsync(language.GetString("command.nopermission")); return; } // get all the data from the database ApplicationTB apptb = statecollection.GetApplicationEntry(Context.Guild, database); SocketTextChannel pc = statecollection.GetPublicChannel(Context.Guild, database, gtb); SocketTextChannel nc = statecollection.GetNotificationChannel(Context.Guild, database, gtb); EmbedBuilder eb = new EmbedBuilder() .WithColor(Color.Gold) .WithTitle($":clipboard: Status information for '{gtb.Name}'"); eb.AddField("Language", gtb.Language); eb.AddField("Public channel", (pc != null ? pc.Mention : "Undefined")); eb.AddField("Notification channel", (nc != null ? nc.Mention : "Undefined")); eb.AddField("Application", (apptb != null ? $"ends on: {apptb.Deadline.ToString("dd MMMM a\\t hh:mm tt UTC")}" : "No active application")); await Context.Channel.SendMessageAsync(statecollection.GetLanguage(Context.Guild, database, gtb).GetString("present"), embed : eb.Build()); } }
public async Task <bool> StopApplication(SocketGuild guild, GuildDB database, ApplicationTB appentry) { // make sure that there is indeed a state with a token if (!guildCollection.ContainsKey(guild.Id)) { return(false); } // cancel notifier logger.Log(new LogMessage(LogSeverity.Info, "State", $"Cancelling application notifier for '{guild.Name}'")); if (!CancelApplicationNotifier(guild)) { return(false); } // delete the invite logger.Log(new LogMessage(LogSeverity.Info, "State", $"Deleting invite for '{guild.Name}'.")); await ExpireAppLink(await GetApplicationInvite(guild, database, appentry)); // delete the channel logger.Log(new LogMessage(LogSeverity.Info, "State", $"Deleting application channel for '{guild.Name}'")); await DeleteApplicationChannel(guild, appentry); // delete from the database RemoveApplicationFromDatabase(appentry, database); return(true); }
public async Task <IInviteMetadata> GetApplicationInvite(SocketGuild guild, GuildDB database, ApplicationTB application = null, SocketTextChannel channel = null) { // make sure that there are indeed applications if (!GetApplicationEntry(guild, database, ref application)) { return(null); } // get the channel where the invite comes from if (channel == null) { channel = GetChannel(guild, application.Channel); if (channel == null) { return(null); } } IEnumerable <IInviteMetadata> invites; try { // try to get the invites from discord invites = await channel.GetInvitesAsync(); } catch (Exception e) { // log failure and return logger.Log(new LogMessage(LogSeverity.Warning, "State", $"Attempted to read invites from '{guild.Name}', but failed: {e.Message}\n{e.StackTrace}")); return(null); } // return the invite return(invites.FirstOrDefault(x => x.Id == application.InviteID)); }
public SocketTextChannel GetApplicationChannel(SocketGuild guild, GuildDB database, ApplicationTB application = null) { // make sure that there are indeed applications if (!GetApplicationEntry(guild, database, ref application)) { return(null); } // return the channel return(GetChannel(guild, application.Channel)); }