示例#1
0
        private async Task <string> SanitiseChannelMentions(string s, Optional <Snowflake> guildId)
        {
            if (!guildId.HasValue)
            {
                return(s);
            }

            var getGuildChannelsRes = await _guildApi.GetGuildChannelsAsync(guildId.Value);

            var sanitised = getGuildChannelsRes.IsSuccess
                ? Regex.Replace(s, ChannelMentionRegex, match =>
            {
                var parseSuccessful = ulong.TryParse(match.Groups[1].Value, out var id);

                if (!parseSuccessful)
                {
                    return($"#{FallbackChannelName}");
                }

                var matchingChannel = getGuildChannelsRes.Entity
                                      .FirstOrDefault(c => c.ID.Value == id);
                var matchingChannelName = matchingChannel switch
                {
                    { Name: { HasValue: true } } => matchingChannel.Name.Value,

                    _ => FallbackChannelName
                };

                return($"#{matchingChannelName}");
            })
示例#2
0
        /// <summary>
        /// Finds a Discord channel in a Discord guild by channel name.
        /// </summary>
        /// <param name="guildApi">The guild API.</param>
        /// <param name="guildId">The ID of the guild in which to look for the channel.</param>
        /// <param name="channelName">The name of the channel to find.</param>
        /// <returns>Result of the operation. If successful contains the matching <see cref="IChannel"/>.</returns>
        /// <remarks>If multiple channels are found returns the first match.</remarks>
        public static async Task <Result <IChannel> > FindGuildChannelByName(this IDiscordRestGuildAPI guildApi,
                                                                             Snowflake guildId,
                                                                             string channelName)
        {
            var getGuildChannelsResult = await guildApi.GetGuildChannelsAsync(guildId);

            if (!getGuildChannelsResult.IsSuccess)
            {
                return(Result <IChannel> .FromError(getGuildChannelsResult));
            }

            var guildChannels = getGuildChannelsResult.Entity;

            if (guildChannels is null)
            {
                return(new NotFoundError("Guild channels for current guild not found."));
            }

            var channel = guildChannels
                          .Where(c => c.Name.HasValue && c.Name.Value is not null)
                          .FirstOrDefault(c => c.Name.Value !.Equals(channelName));

            if (channel is null)
            {
                return(new NotFoundError(
                           $"Could not find the channel with name {channelName} in guild {guildId}."));
            }

            return(Result <IChannel> .FromSuccess(channel));
        }
示例#3
0
            public async Task <Result <QueryResult> > Handle(Query request, CancellationToken cancellationToken)
            {
                var getGuildRolesResult = await _guildApi.GetGuildRolesAsync(request.GuildId, cancellationToken);

                if (!getGuildRolesResult.IsSuccess)
                {
                    return(Result <QueryResult> .FromError(getGuildRolesResult));
                }

                var getGuildChannelsResult = await _guildApi.GetGuildChannelsAsync(request.GuildId, cancellationToken);

                if (!getGuildChannelsResult.IsSuccess)
                {
                    return(Result <QueryResult> .FromError(getGuildChannelsResult));
                }

                var roles = getGuildRolesResult.Entity;

                if (roles is null)
                {
                    return(new NotFoundError($"Could not find roles in guild {request.GuildId}."));
                }

                var channels = getGuildChannelsResult.Entity;

                if (channels is null)
                {
                    return(new NotFoundError($"Could not find channels in guild {request.GuildId}."));
                }

                var staffRole = roles.FirstOrDefault(r => r.Name.Equals(_roleNames.Staff));

                if (staffRole is null)
                {
                    return(new NotFoundError(
                               $"Could not find Staff role ({_roleNames.Staff}) in guild {request.GuildId}."));
                }

                var membersChannel = channels.FirstOrDefault(c => c.Name.Equals(_channelNames.MemberApps));

                if (membersChannel is null)
                {
                    return(new NotFoundError(
                               $"Could not find member apps channel (#{_channelNames.MemberApps}) in guild {request.GuildId}."));
                }

                return(new QueryResult(membersChannel.ID, staffRole.ID));
            }