public async Task UpdateGuildConfigAsync(GuildConfig config) { if (!GuildConfigs.TryAdd(config.GuildId, config)) { GuildConfigs[config.GuildId] = config; } await DatabaseHandler.UpdateGuildConfigDatabaseAsync(config).ConfigureAwait(false); }
private async Task <int> PrefixResolver(DiscordMessage msg) { if (!msg.Channel.PermissionsFor(await msg.Channel.Guild.GetMemberAsync(Client.CurrentUser.Id).ConfigureAwait(false)).HasPermission(Permissions.SendMessages)) { return(-1); //Checks if bot can't send messages, if so ignore. } if (msg.Content.StartsWith(Config.Prefix)) { return(Config.Prefix.Length); //Always respond with default prefix. } else { try { if (GuildConfigs.TryGetValue(msg.Channel.GuildId, out GuildConfig gConfig)) { // Do nothing, we just want the value. } else { gConfig = await DatabaseHandler.GetSingleGuildConfigAsync(msg.Channel.GuildId).ConfigureAwait(false); if (gConfig is null) { gConfig = new GuildConfig { GuildId = msg.Channel.GuildId, Prefix = Config.Prefix }; _ = UpdateGuildConfigAsync(gConfig).ConfigureAwait(false); Client.Logger.LogInformation(Program.PrefixManager, $"Resolver generated deafult config for: {msg.Channel.Guild.Name}", DateTime.Now); return(-1); } else { GuildConfigs.TryAdd(gConfig.GuildId, gConfig); } } foreach (string cmd in CommandList) //Loop through all current commands. { if (msg.Content.StartsWith(gConfig.Prefix + cmd)) //Check if message starts with prefix AND command. { return(gConfig.Prefix.Length); //Return length of server prefix. } } return(-1); //If not, ignore. } catch (Exception err) { Client.Logger.LogError(Program.PrefixManager, $"Resolver failed in guild {msg.Channel.Guild.Name}:", DateTime.Now, err); return(-1); } } }
public void EnsureCorrectDatabaseState() { foreach (var gc in GuildConfigs.Include(g => g.LogSetting)) { if (gc.LogSetting == null) { gc.LogSetting = new LogSetting(); GuildConfigs.Update(gc); } } SaveChanges(); }
/// <summary> /// Refreshes config file guilds /// </summary> public void RefreshGuilds() { foreach (SocketGuild guild in Program._client.Guilds) { if (GuildConfigs.ContainsKey(guild.Id)) { GuildConfigs[guild.Id].RefreshUsers(); } else { CreateGuild(guild.Id); GuildConfigs[guild.Id].RefreshUsers(); } } }
public IBotServerConfiguration GetServerConfig(ulong id, ChatTypes chatType, string name = "") { var result = GuildConfigs.FirstOrDefault(x => x.Id == id && (x.ChatType ?? ChatTypes.Discord) == chatType); if (result == null) { result = new ServerConfiguration() { Id = id, ChatType = chatType, Name = name }; GuildConfigs.Add(result); } result.ChatType = chatType; return(result); }
protected async Task OnGuildAvaivable(SocketGuild guild) { GuildConfig config = default; if (await GuildConfigs.CountAsync() != 0) { config = await GuildConfigs.FirstAsync(g => g.GuildId == guild.Id); } if (config == null) { config = new GuildConfig() { GuildId = guild.Id }; Add(config); await SaveChangesAsync(); } }
private void CreateGuild(ulong guildId) { GuildConfig guildConfig = new GuildConfig(guildId); GuildConfigs.Add(guildConfig.GuildId, guildConfig); }
public bool HasGuildConfig(ulong id) { return(GuildConfigs.Where(x => x.Id == id).Count() > 0); }