public async Task AddZoneAlias(string zoneName, string alias) { IZone zone = await this.GetZoneOrReplyAsync(zoneName); if (zone.IsValid()) { alias = ZoneUtilities.GetFullName(alias); if (zone.GetFullName().Equals(alias, StringComparison.OrdinalIgnoreCase) || zone.Aliases.Contains(alias, StringComparer.OrdinalIgnoreCase)) { // The zone already has the given name/alias. await ReplyWarningAsync($"{zone.GetFullName().ToBold()} already has this name."); } else if ((await Db.GetZoneAsync(alias)).IsValid()) { // Another zone already has the given name/alias. await ReplyErrorAsync($"There is already a zone named {alias.ToBold()}."); } else { zone.Aliases.Add(alias); await Db.UpdateZoneAsync(zone); await ReplySuccessAsync($"{alias.ToBold()} was successfully added as an alias for {zone.GetFullName().ToBold()}."); } } }
public static async Task <bool> ReplyValidateZoneAsync(this OfcModuleBase moduleBase, IZone zone, string zoneName = "") { if (!zone.IsValid()) { string message = "No such zone exists."; if (!string.IsNullOrEmpty(zoneName)) { zoneName = ZoneUtilities.GetFullName(zoneName); if (zoneName.StartsWith("zone", System.StringComparison.OrdinalIgnoreCase)) { message = $"{zoneName.ToTitle().ToBold()} does not exist."; } else { message = $"Zone {zoneName.ToTitle().ToBold()} does not exist."; } } await DiscordUtilities.ReplyErrorAsync(moduleBase.Context.Channel, message); return(false); } return(true); }
public async Task SetZoneName(string oldZoneName, string newZoneName) { IZone zone = await this.GetZoneOrReplyAsync(oldZoneName); if (zone.IsValid()) { oldZoneName = zone.GetFullName(); newZoneName = ZoneUtilities.GetFullName(newZoneName); if ((await Db.GetZoneAsync(newZoneName)).IsValid()) { // The given name cannot be the name of an existing zone. await ReplyErrorAsync($"There is already a zone named {newZoneName.ToBold()}."); } else { if (oldZoneName.Equals(newZoneName, System.StringComparison.OrdinalIgnoreCase)) { await ReplyWarningAsync($"{zone.GetFullName().ToBold()} already has this name."); } else { zone.Name = newZoneName; await Db.UpdateZoneAsync(zone); await ReplySuccessAsync($"{oldZoneName.ToBold()} was successfully renamed to {newZoneName.ToBold()}."); } } } }
private async Task <IEnumerable <string> > GetMissingRolesInZoneIdeasAsync() { // Checks for roles that are unfulfilled for a given zone List <string> ideas = new List <string>(); string query = @"SELECT Zones.id AS zone_id1, Zones.name AS zone_name, Zones.flags AS zone_flags, Roles.id AS role_id1, Roles.name AS role_name FROM Zones, Roles WHERE NOT EXISTS(SELECT * FROM SpeciesRoles WHERE role_id = role_id1 AND species_id IN (SELECT species_id FROM SpeciesZones WHERE zone_id = zone_id1));" ; using (SQLiteCommand cmd = new SQLiteCommand(query)) { foreach (DataRow row in await Db.GetRowsAsync(cmd)) { ZoneFlags zoneFlags = ZoneFlags.None; if (!row.IsNull("zone_flags")) { zoneFlags = (ZoneFlags)row.Field <long>("zone_flags"); } if (!zoneFlags.HasFlag(ZoneFlags.Retired)) { string zoneName = row.Field <string>("zone_name"); string roleName = row.Field <string>("role_name"); ideas.Add($"{ZoneUtilities.GetFullName(zoneName).ToBold()} does not have any {roleName.ToTitle().ToPlural().ToBold()}. Why not fill this role?"); } } } return(ideas.ToArray()); }
public async Task AddZone(string zoneName, string arg1 = "", string arg2 = "") { // Cases: // 1. <zoneName> // 2. <zoneName> <zoneTypeName> // 3. <zoneName> <zoneTypeName> <description> // 4. <zoneName> <description> string zoneTypeName = arg1; string description = arg2; if (string.IsNullOrEmpty(zoneName)) { // The user must specify a non-empty zone name. await ReplyErrorAsync("Zone name cannot be empty."); } else { // Allow the user to specify zones with numbers (e.g. "1") or single letters (e.g. "A"). // Otherwise, the name is taken as-is. zoneName = ZoneUtilities.GetFullName(zoneName).ToLowerInvariant(); IZoneType zoneType = await Db.GetZoneTypeAsync(arg1); if (!zoneType.IsValid()) { // If an invalid type was provided, assume the user meant it as a description instead (4). description = zoneTypeName; // Attempt to determine the zone type automatically based on the zome name. // This is currently only possible for default zones ("aquatic" or "terrestrial"). zoneType = await Db.GetDefaultZoneTypeAsync(zoneName); } if (await Db.GetZoneAsync(zoneName) != null) { // Don't attempt to create the zone if it already exists. await ReplyWarningAsync($"A zone named {ZoneUtilities.GetFullName(zoneName).ToBold()} already exists."); } else { // Add the new zone. await Db.AddZoneAsync(new Zone { Name = zoneName, Description = description, TypeId = zoneType.Id }); await ReplySuccessAsync($"Successfully created new {zoneType.Name.ToLowerInvariant()} zone, {ZoneUtilities.GetFullName(zoneName).ToBold()}."); } } }
private static async Task AddZoneAliasesAsync(this SQLiteDatabase database, IZone zone) { foreach (string alias in zone.Aliases.Select(alias => ZoneUtilities.GetFullName(alias).ToLowerInvariant()).Distinct()) { using (SQLiteCommand cmd = new SQLiteCommand("INSERT OR IGNORE INTO ZoneAliases(zone_id, alias) VALUES($zone_id, $alias)")) { cmd.Parameters.AddWithValue("$zone_id", zone.Id); cmd.Parameters.AddWithValue("$alias", alias.ToLowerInvariant().SafeTrim()); await database.ExecuteNonQueryAsync(cmd); } } }
public static async Task <IZone> GetZoneAsync(this SQLiteDatabase database, string name, GetZoneOptions options = GetZoneOptions.Default) { if (string.IsNullOrWhiteSpace(name)) { return(null); } string fullName = ZoneUtilities.GetFullName(name.Trim()).ToLowerInvariant(); using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Zones WHERE name = $name OR name = $fullName OR id IN (SELECT zone_id FROM ZoneAliases WHERE alias = $name OR alias = $fullName)")) { cmd.Parameters.AddWithValue("$name", name.ToLowerInvariant()); cmd.Parameters.AddWithValue("$fullName", fullName.ToLowerInvariant()); DataRow row = await database.GetRowAsync(cmd); if (row != null) { return(await database.CreateZoneFromDataRowAsync(row, options)); } } return(null); }
public static string GetFullName(this IZone zone) { return(ZoneUtilities.GetFullName(zone.Name)); }