Esempio n. 1
0
        private async Task <ITextChannel> GetReportChannelAsync(SocketGuild guild)
        {
            // Attempts to get text channel from database entry.
            var record = await CoreSettings.GetOrCreateGuildSettingsAsync(guild).ConfigureAwait(false);

            var reportChannel = guild.GetTextChannel(record.ReportChannel);

            if (reportChannel != null)
            {
                return(reportChannel);
            }

            // Attempts to get text channel by name.
            var searchChannel = guild.TextChannels.FirstOrDefault(x => x.Name.ToLower().Contains(ReportChannelName));

            if (searchChannel != null)
            {
                record.ReportChannel = searchChannel.Id;
                await CoreSettings.SaveRepositoryAsync().ConfigureAwait(false);

                return(searchChannel);
            }

            // Attempts to create a new text channel.
            var newChannel = await guild.CreateTextChannelAsync(ReportChannelName).ConfigureAwait(false);

            await newChannel.AddPermissionOverwriteAsync(guild.EveryoneRole,
                                                         new OverwritePermissions(viewChannel : PermValue.Deny)).ConfigureAwait(false);

            var modRoles = await GetModeratorRolesAsync(guild).ConfigureAwait(false);

            foreach (var modRole in modRoles)
            {
                await newChannel.AddPermissionOverwriteAsync(modRole,
                                                             new OverwritePermissions(viewChannel : PermValue.Allow)).ConfigureAwait(false);
            }
            return(newChannel);
        }