mirror of
				https://gitlab.com/Kwoth/nadekobot.git
				synced 2025-11-03 16:24:27 -05:00 
			
		
		
		
	- .say now uses new SmartText instead of CREmbed
- Added IMessageChannel extensions for sending smarttext - Added implicit operator from string to smarttext (which just creates smartplaintext instance)
This commit is contained in:
		@@ -34,6 +34,58 @@ namespace NadekoBot.Common.Replacements
 | 
				
			|||||||
            return input;
 | 
					            return input;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public SmartText Replace(SmartText data)
 | 
				
			||||||
 | 
					            => data switch
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                SmartEmbedText embedData => Replace(embedData),
 | 
				
			||||||
 | 
					                SmartPlainText plain => Replace(plain),
 | 
				
			||||||
 | 
					                _ => throw new ArgumentOutOfRangeException(nameof(data), "Unsupported argument type")
 | 
				
			||||||
 | 
					            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public SmartPlainText Replace(SmartPlainText plainText)
 | 
				
			||||||
 | 
					            => Replace(plainText.Text);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public SmartEmbedText Replace(SmartEmbedText embedData)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            var newEmbedData = new SmartEmbedText();
 | 
				
			||||||
 | 
					            newEmbedData.PlainText = Replace(embedData.PlainText);
 | 
				
			||||||
 | 
					            newEmbedData.Description = Replace(embedData.Description);
 | 
				
			||||||
 | 
					            newEmbedData.Title = Replace(embedData.Title);
 | 
				
			||||||
 | 
					            newEmbedData.Thumbnail = Replace(embedData.Thumbnail);
 | 
				
			||||||
 | 
					            newEmbedData.Image = Replace(embedData.Image);
 | 
				
			||||||
 | 
					            if (embedData.Author != null)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                newEmbedData.Author = new SmartTextEmbedAuthor();
 | 
				
			||||||
 | 
					                newEmbedData.Author.Name = Replace(embedData.Author.Name);
 | 
				
			||||||
 | 
					                newEmbedData.Author.IconUrl = Replace(embedData.Author.IconUrl);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (embedData.Fields != null)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                var fields = new List<SmartTextEmbedField>();
 | 
				
			||||||
 | 
					                foreach (var f in embedData.Fields)
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    var newF = new SmartTextEmbedField();
 | 
				
			||||||
 | 
					                    newF.Name = Replace(f.Name);
 | 
				
			||||||
 | 
					                    newF.Value = Replace(f.Value);
 | 
				
			||||||
 | 
					                    fields.Add(newF);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                newEmbedData.Fields = fields.ToArray();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (embedData.Footer != null)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                newEmbedData.Footer = new SmartTextEmbedFooter();
 | 
				
			||||||
 | 
					                newEmbedData.Footer.Text = Replace(embedData.Footer.Text);
 | 
				
			||||||
 | 
					                newEmbedData.Footer.IconUrl = Replace(embedData.Footer.IconUrl);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            newEmbedData.Color = embedData.Color;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            return newEmbedData;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public CREmbed Replace(CREmbed embedData)
 | 
					        public CREmbed Replace(CREmbed embedData)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            embedData.PlainText = Replace(embedData.PlainText);
 | 
					            embedData.PlainText = Replace(embedData.PlainText);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,15 +2,17 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace NadekoBot
 | 
					namespace NadekoBot
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    // todo 3.3 check if saving embeds in db has IsEmbed field, to prevent rechecking and generating exceptions on every use
 | 
					 | 
				
			||||||
    public abstract class SmartText
 | 
					    public abstract class SmartText
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        public bool IsEmbed => this is SmartEmbedText;
 | 
					        public bool IsEmbed => this is SmartEmbedText;
 | 
				
			||||||
        public bool IsPlainText => this is SmartPlainText;
 | 
					        public bool IsPlainText => this is SmartPlainText;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public static implicit operator SmartText(string input)
 | 
				
			||||||
 | 
					            => new SmartPlainText(input);
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
        public static SmartText CreateFrom(string input)
 | 
					        public static SmartText CreateFrom(string input)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            if (string.IsNullOrWhiteSpace(input) || !input.Trim().StartsWith("{"))
 | 
					            if (string.IsNullOrWhiteSpace(input) || !input.TrimStart().StartsWith("{"))
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                return new SmartPlainText(input);
 | 
					                return new SmartPlainText(input);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,4 +13,12 @@ namespace NadekoBot.Common.TypeReaders
 | 
				
			|||||||
            return Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Input is not a valid kwum"));
 | 
					            return Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Input is not a valid kwum"));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public sealed class SmartTextTypeReader : NadekoTypeReader<SmartText>
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        public override Task<TypeReaderResult> ReadAsync(ICommandContext ctx, string input)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            return Task.FromResult(TypeReaderResult.FromSuccess(SmartText.CreateFrom(input)));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -41,36 +41,23 @@ namespace NadekoBot.Modules.Utility
 | 
				
			|||||||
        [RequireContext(ContextType.Guild)]
 | 
					        [RequireContext(ContextType.Guild)]
 | 
				
			||||||
        [UserPerm(GuildPerm.ManageMessages)]
 | 
					        [UserPerm(GuildPerm.ManageMessages)]
 | 
				
			||||||
        [Priority(1)]
 | 
					        [Priority(1)]
 | 
				
			||||||
        public async Task Say(ITextChannel channel, [Leftover] string message)
 | 
					        public async Task Say(ITextChannel channel, [Leftover] SmartText message)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            if (string.IsNullOrWhiteSpace(message))
 | 
					 | 
				
			||||||
                return;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            var rep = new ReplacementBuilder()
 | 
					            var rep = new ReplacementBuilder()
 | 
				
			||||||
                .WithDefault(ctx.User, channel, (SocketGuild)ctx.Guild, (DiscordSocketClient)ctx.Client)
 | 
					                .WithDefault(ctx.User, channel, (SocketGuild)ctx.Guild, (DiscordSocketClient)ctx.Client)
 | 
				
			||||||
                .Build();
 | 
					                .Build();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (CREmbed.TryParse(message, out var embedData))
 | 
					            rep.Replace(message);
 | 
				
			||||||
            {
 | 
					            
 | 
				
			||||||
                rep.Replace(embedData);
 | 
					            await channel.SendAsync(_eb, message, !((IGuildUser)Context.User).GuildPermissions.MentionEveryone);
 | 
				
			||||||
                await channel.EmbedAsync(embedData, _eb, sanitizeAll: !((IGuildUser)Context.User).GuildPermissions.MentionEveryone).ConfigureAwait(false);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            else
 | 
					 | 
				
			||||||
            {
 | 
					 | 
				
			||||||
                var msg = rep.Replace(message);
 | 
					 | 
				
			||||||
                if (!string.IsNullOrWhiteSpace(msg))
 | 
					 | 
				
			||||||
                {
 | 
					 | 
				
			||||||
                    await channel.SendConfirmAsync(_eb, msg).ConfigureAwait(false);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        [NadekoCommand, Aliases]
 | 
					        [NadekoCommand, Aliases]
 | 
				
			||||||
        [RequireContext(ContextType.Guild)]
 | 
					        [RequireContext(ContextType.Guild)]
 | 
				
			||||||
        [UserPerm(GuildPerm.ManageMessages)]
 | 
					        [UserPerm(GuildPerm.ManageMessages)]
 | 
				
			||||||
        [Priority(0)]
 | 
					        [Priority(0)]
 | 
				
			||||||
        public Task Say([Leftover] string message) =>
 | 
					        public Task Say([Leftover] string message)
 | 
				
			||||||
            Say((ITextChannel)ctx.Channel, message);
 | 
					            => Say((ITextChannel)ctx.Channel, message);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        [NadekoCommand, Aliases]
 | 
					        [NadekoCommand, Aliases]
 | 
				
			||||||
        [RequireContext(ContextType.Guild)]
 | 
					        [RequireContext(ContextType.Guild)]
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -35,6 +35,23 @@ namespace NadekoBot.Extensions
 | 
				
			|||||||
        public static TOut[] Map<TIn, TOut>(this TIn[] arr, Func<TIn, TOut> f)
 | 
					        public static TOut[] Map<TIn, TOut>(this TIn[] arr, Func<TIn, TOut> f)
 | 
				
			||||||
            => Array.ConvertAll(arr, x => f(x));
 | 
					            => Array.ConvertAll(arr, x => f(x));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public static Task<IUserMessage> SendAsync(this IMessageChannel channel, string plainText, Embed embed, bool sanitizeAll = false)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            plainText = sanitizeAll
 | 
				
			||||||
 | 
					                ? plainText?.SanitizeAllMentions() ?? ""
 | 
				
			||||||
 | 
					                : plainText?.SanitizeMentions() ?? "";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            return channel.SendMessageAsync(plainText, embed: embed);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        public static Task<IUserMessage> SendAsync(this IMessageChannel channel, IEmbedBuilderService eb, SmartText text, bool sanitizeAll = false)
 | 
				
			||||||
 | 
					            => text switch
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                SmartEmbedText set => channel.SendAsync(set.PlainText, set.GetEmbed(eb).Build(), sanitizeAll),
 | 
				
			||||||
 | 
					                SmartPlainText st => channel.SendAsync(st.Text, null, sanitizeAll),
 | 
				
			||||||
 | 
					                _ => throw new ArgumentOutOfRangeException(nameof(text))
 | 
				
			||||||
 | 
					            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public static Task<IUserMessage> EmbedAsync(this IMessageChannel channel, CREmbed crEmbed, IEmbedBuilderService eb, bool sanitizeAll = false)
 | 
					        public static Task<IUserMessage> EmbedAsync(this IMessageChannel channel, CREmbed crEmbed, IEmbedBuilderService eb, bool sanitizeAll = false)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            var plainText = sanitizeAll
 | 
					            var plainText = sanitizeAll
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user