public static Language GetDefaultLanguage(IGuild guild, IChannel channel = null, string specifiedLanguage = null) { // Check if a language was specified if (specifiedLanguage == null) { // Check if this is not a DM if (guild != null) { // Attempt to get the GuildSettings for this guild GuildSettings guildSettings = Configuration.LoadedConfiguration.DiscordConfig.GuildSettings.Where(x => x.GuildId == guild.Id).FirstOrDefault(); // Check if there is a GuildSettings if (guildSettings != null) { // Check if there is a channel specified if (channel != null) { // Attempt to get a DynamicSettingsData for this channel DynamicSettingsData channelSettings = guildSettings.ChannelSettings.Where(c => c.Key == channel.Id).FirstOrDefault().Value; // Check if it exists if (channelSettings != null) { // Return the channel's language return((Language)channelSettings.GetSetting("language")); } } // Return the guild's default language return((Language)guildSettings.GetSetting("default_language")); } } // Default to en-US return(Language.EnglishUS); } else { try { return(LanguageExtensions.FromCode(specifiedLanguage)); } catch (Exception) { throw new LocalizedException("discord.error.bad_code"); } } }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { // Get the target Dictionary if it exists Dictionary <Language, string> textMappings = existingValue != null ? (Dictionary <Language, string>)existingValue : new Dictionary <Language, string>(); // Get the object JObject jObject = JObject.Load(reader); // Loop over each propery foreach (JProperty jProperty in jObject.Properties()) { // Add this property's key and value to the Dictionary textMappings.Add(LanguageExtensions.FromCode(jProperty.Name), (string)jProperty.Value); } // Return the Dictionary return(textMappings); }
public async Task Register(IGuildChannel channel, string languageCode) { if (Context.Guild == null) { throw new LocalizedException("registration.in_dm"); } // Check that that the user has the manage guild permission if (!((SocketGuildUser)Context.User).GuildPermissions.Has(GuildPermission.ManageGuild)) { throw new LocalizedException("registration.user_no_manage_permission"); } // Check that we can write to this channel first if (!Context.Guild.CurrentUser.GetPermissions(channel).Has(ChannelPermission.SendMessages)) { throw new LocalizedException("registration.bot_no_write_permission"); } // Check the language code Language language; try { language = LanguageExtensions.FromCode(languageCode); } catch (Exception) { throw new LocalizedException("registration.bad_code"); } // Get any existing GuildSettings for this server GuildSettings guildSettings = Configuration.LoadedConfiguration.DiscordConfig.GuildSettings.Where(x => x.GuildId == Context.Guild.Id).FirstOrDefault(); // Check if the GuildSettings doesn't exist if (guildSettings == null) { // Create a GuildSettings instance guildSettings = new GuildSettings(); // Add this to the Configuration Configuration.LoadedConfiguration.DiscordConfig.GuildSettings.Add(guildSettings); } // Set the GuildSettings fields guildSettings.GuildId = Context.Guild.Id; guildSettings.TargetChannelId = channel.Id; guildSettings.DefaultLanguage = language; // Get the localized embed fields string embedTitle = Localizer.Localize("registration.title", guildSettings.DefaultLanguage); string embedDescription = Localizer.Localize("registration.description", guildSettings.DefaultLanguage); // Build the Embed Embed embed = new EmbedBuilder() .WithTitle(embedTitle) .WithDescription(embedDescription) .WithColor(Color.Green) .Build(); // Send the Embed await Context.Channel.SendMessageAsync(embed : embed); await DiscordBot.LoggingChannel.SendMessageAsync($"**[RegistrationCommand]** Registered \"{Context.Guild.Name}\" ({Context.Guild.Id}) to #{channel.Name} ({channel.Id}) using language {language.ToString()}"); }