예제 #1
0
        /// <inheritdoc />
        public async Task UseTagAsync(ulong guildId, ulong channelId, string name)
        {
            AuthorizationService.RequireClaims(AuthorizationClaim.UseTag);

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("The tag name cannot be blank or whitespace.", nameof(name));
            }

            name = name.Trim().ToLower();

            using (var transaction = await TagRepository.BeginUseTransactionAsync())
            {
                var tag = await TagRepository.ReadSummaryAsync(guildId, name);

                if (tag is null)
                {
                    throw new InvalidOperationException($"The tag '{name}' does not exist.");
                }

                var channel = await DiscordClient.GetChannelAsync(channelId);

                if (!(channel is IMessageChannel messageChannel))
                {
                    throw new InvalidOperationException($"The channel '{channel.Name}' is not a message channel.");
                }

                var sanitizedContent = FormatUtilities.Sanitize(tag.Content);

                try
                {
                    await messageChannel.SendMessageAsync(sanitizedContent);
                }
                finally
                {
                    await TagRepository.TryIncrementUsesAsync(guildId, name);
                }

                transaction.Commit();
            }
        }