public async Task <Result> RespondAsync(IGuildCreate ev, CancellationToken ct = default) { _logger.LogInformation($"Setting up guild with ID: \"{ev.ID}\" and Name: \"{ev.Name}\""); var channels = ev.Channels; List <string> requiredChannels = _discordSettings.ChannelNames .GetType() .GetProperties(BindingFlags.Instance | BindingFlags.Public) .Where(p => p.CanRead && p.PropertyType == typeof(string)) .Select(p => p.GetValue(_discordSettings.ChannelNames) as string) .Where(s => s is not null) .ToList() !; if (!channels.HasValue) { foreach (var requiredChannel in requiredChannels) { var createChannelResult = await _guildApi.CreateGuildChannelAsync(ev.ID, requiredChannel, ChannelType.GuildText, ct : ct); if (!createChannelResult.IsSuccess) { return(new SetupError("Failed to create required channels.")); } _logger.LogInformation( $"Created required channel \"{requiredChannel}\" in guild with ID: \"{ev.ID}\" and Name: \"{ev.Name}\""); } } else { // ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator foreach (var requiredChannel in requiredChannels) { // ReSharper disable once InvertIf if (channels.Value.FirstOrDefault(c => c.Name.Value.Equals(requiredChannel)) is null) { var createChannelResult = await _guildApi.CreateGuildChannelAsync(ev.ID, requiredChannel, ChannelType.GuildText, ct : ct); if (!createChannelResult.IsSuccess) { return(new SetupError("Failed to create required channels.")); } _logger.LogInformation( $"Created required channel \"{requiredChannel}\" in guild with ID: \"{ev.ID}\" and Name: \"{ev.Name}\""); } } } _logger.LogInformation($"Successfully set up guild with ID: \"{ev.ID}\" and Name: \"{ev.Name}\""); return(Result.FromSuccess()); }
/// <summary> /// Creates a dedicated channel for the roleplay. /// </summary> /// <param name="roleplay">The roleplay to create the channel for.</param> /// <returns>A modification result which may or may not have succeeded.</returns> public async Task <Result <IChannel> > CreateDedicatedChannelAsync(Roleplay roleplay) { var getExistingChannelResult = GetDedicatedChannel(roleplay); if (getExistingChannelResult.IsSuccess) { return(new UserError ( "The roleplay already has a dedicated channel." )); } var getSettingsResult = await _serverSettings.GetOrCreateServerRoleplaySettingsAsync ( roleplay.Server.DiscordID ); if (!getSettingsResult.IsSuccess) { return(Result <IChannel> .FromError(getSettingsResult)); } var settings = getSettingsResult.Entity; if (settings.DedicatedRoleplayChannelsCategory is null) { return(new UserError ( "No dedicated channel category has been configured." )); } var createChannel = await _guildAPI.CreateGuildChannelAsync ( roleplay.Server.DiscordID, $"{roleplay.Name}-rp", ChannelType.GuildText, parentID : settings.DedicatedRoleplayChannelsCategory.Value, isNsfw : roleplay.IsNSFW, topic : $"Dedicated roleplay channel for {roleplay.Name}. {roleplay.Summary}" ); if (!createChannel.IsSuccess) { if (createChannel.Error is not RestResultError <RestError> rre) { return(Result <IChannel> .FromError(createChannel)); } return(rre.Error.Code switch { DiscordError.MissingPermission => new UserError ( "I don't have permission to manage channels, so I can't create dedicated RP channels." ), _ => Result <IChannel> .FromError(createChannel) });