From 96ad5516d38d4b1d5313f766bce18ed0fa5a999b Mon Sep 17 00:00:00 2001 From: Kwoth Date: Fri, 3 May 2024 02:59:34 +0000 Subject: [PATCH] .send commmand reworked, It should be much better and more reliable now. --- .../Administration/Self/SelfCommands.cs | 83 +++++++++---------- .../_common/Sender/ResponseBuilder.cs | 8 +- .../data/strings/commands/commands.en-US.yml | 6 +- 3 files changed, 47 insertions(+), 50 deletions(-) diff --git a/src/NadekoBot/Modules/Administration/Self/SelfCommands.cs b/src/NadekoBot/Modules/Administration/Self/SelfCommands.cs index 7c262928d..070aa4de7 100644 --- a/src/NadekoBot/Modules/Administration/Self/SelfCommands.cs +++ b/src/NadekoBot/Modules/Administration/Self/SelfCommands.cs @@ -1,4 +1,5 @@ #nullable disable +using Discord.Rest; using NadekoBot.Modules.Administration.Services; using NadekoBot.Db.Models; using Nadeko.Common.Medusa; @@ -62,9 +63,9 @@ public partial class Administration await message.ModifyAsync(x => x.Embed = _sender.CreateEmbed() - .WithDescription(GetText(strs.cache_users_done(added, updated))) - .WithOkColor() - .Build() + .WithDescription(GetText(strs.cache_users_done(added, updated))) + .WithOkColor() + .Build() ); } @@ -80,7 +81,7 @@ public partial class Administration && ctx.Message is SocketUserMessage msg) { var fakeMessage = new DoAsUserMessage(msg, user, message); - + await _cmdHandler.TryRunCommand(sg, ch, fakeMessage); } @@ -115,13 +116,13 @@ public partial class Administration await Response() .Embed(_sender.CreateEmbed() - .WithOkColor() - .WithTitle(GetText(strs.scadd)) - .AddField(GetText(strs.server), - cmd.GuildId is null ? "-" : $"{cmd.GuildName}/{cmd.GuildId}", - true) - .AddField(GetText(strs.channel), $"{cmd.ChannelName}/{cmd.ChannelId}", true) - .AddField(GetText(strs.command_text), cmdText)) + .WithOkColor() + .WithTitle(GetText(strs.scadd)) + .AddField(GetText(strs.server), + cmd.GuildId is null ? "-" : $"{cmd.GuildName}/{cmd.GuildId}", + true) + .AddField(GetText(strs.channel), $"{cmd.ChannelName}/{cmd.ChannelId}", true) + .AddField(GetText(strs.command_text), cmdText)) .SendAsync(); } @@ -546,51 +547,41 @@ public partial class Administration await Response().Confirm(strs.set_stream).SendAsync(); } + public enum SendWhere + { + User = 0, + U = 0, + Usr = 0, + + Channel = 1, + Ch = 1, + Chan = 1, + } + [Cmd] [OwnerOnly] - public async Task Send(string where, [Leftover] SmartText text = null) + public async Task Send(SendWhere to, ulong id, [Leftover] SmartText text) { - var ids = where.Split('|'); - if (ids.Length != 2) - return; - - var sid = ulong.Parse(ids[0]); - var server = _client.Guilds.FirstOrDefault(s => s.Id == sid); - - if (server is null) - return; - - // var repSvc = new ReplacementBuilder().WithDefault(Context).Build(); - var repCtx = new ReplacementContext(Context); - - if (ids[1].ToUpperInvariant().StartsWith("C:", StringComparison.InvariantCulture)) + var ch = to switch { - var cid = ulong.Parse(ids[1][2..]); - var ch = server.TextChannels.FirstOrDefault(c => c.Id == cid); - if (ch is null) - return; + SendWhere.User => await ((await _client.Rest.GetUserAsync(id))?.CreateDMChannelAsync() + ?? Task.FromResult(null)), + SendWhere.Channel => await _client.Rest.GetChannelAsync(id) as IMessageChannel, + _ => null + }; - text = await repSvc.ReplaceAsync(text, repCtx); - await Response().Channel(ch).Text(text).SendAsync(); - } - else if (ids[1].ToUpperInvariant().StartsWith("U:", StringComparison.InvariantCulture)) - { - var uid = ulong.Parse(ids[1][2..]); - var user = server.Users.FirstOrDefault(u => u.Id == uid); - if (user is null) - return; - - var ch = await user.CreateDMChannelAsync(); - text = await repSvc.ReplaceAsync(text, repCtx); - await Response().Channel(ch).Text(text).SendAsync(); - } - else + if (ch is null) { await Response().Error(strs.invalid_format).SendAsync(); return; } - await Response().Confirm(strs.message_sent).SendAsync(); + + var repCtx = new ReplacementContext(ctx); + text = await repSvc.ReplaceAsync(text, repCtx); + await Response().Channel(ch).Text(text).SendAsync(); + + await ctx.OkAsync();; } [Cmd] diff --git a/src/NadekoBot/_common/Sender/ResponseBuilder.cs b/src/NadekoBot/_common/Sender/ResponseBuilder.cs index ef37fcc93..598c2b2b3 100644 --- a/src/NadekoBot/_common/Sender/ResponseBuilder.cs +++ b/src/NadekoBot/_common/Sender/ResponseBuilder.cs @@ -466,7 +466,13 @@ public sealed class SourcedPaginatedResponseBuilder : PaginatedResponseBuilde public SourcedPaginatedResponseBuilder Interaction(Func> func) { - InteractionFunc = async (i) => await func(i); + InteractionFunc = func; //async (i) => await func(i); + return this; + } + + public SourcedPaginatedResponseBuilder Interaction(SimpleInteractionBase inter) + { + InteractionFunc = _ => Task.FromResult(inter); return this; } } \ No newline at end of file diff --git a/src/NadekoBot/data/strings/commands/commands.en-US.yml b/src/NadekoBot/data/strings/commands/commands.en-US.yml index bc12fa549..5a2ec98bb 100644 --- a/src/NadekoBot/data/strings/commands/commands.en-US.yml +++ b/src/NadekoBot/data/strings/commands/commands.en-US.yml @@ -413,10 +413,10 @@ setgame: - "Watching anime." - "Listening music." send: - desc: "Sends a message to someone on a different server through the bot. Separate server and channel/user ids with `|` and prefix the channel id with `c:` and the user id with `u:`." + desc: "Sends a message to a channel or user. Channel or user can be " args: - - "serverid|c:channelid message" - - "serverid|u:userid message" + - "channel 123123123132312 Stop spamming commands plz" + - "user 1231231232132 I can see in the console what you're doing." savechat: desc: "Saves a number of messages to a text file and sends it to you." args: