From 29de185cbae35bd4ac198edff3fbd0fc0b021419 Mon Sep 17 00:00:00 2001 From: balos Date: Mon, 28 Apr 2025 21:36:25 +0200 Subject: [PATCH] added sendmsg command --- commands/hello.cs | 2 +- commands/send.cs | 28 +++++++++++++++++++++ idextractor.cs | 62 +++++++++++++++++++++++++++++++++++++++++++++++ program.cs | 5 ++-- 4 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 commands/send.cs create mode 100644 idextractor.cs diff --git a/commands/hello.cs b/commands/hello.cs index 6acbcf3..7a2c5b6 100644 --- a/commands/hello.cs +++ b/commands/hello.cs @@ -2,7 +2,7 @@ using DSharpPlus.CommandsNext; using DSharpPlus.CommandsNext.Attributes; using System.Threading.Tasks; -namespace Commands +namespace T3k3rg0.Commands { public class Hello : BaseCommandModule { diff --git a/commands/send.cs b/commands/send.cs new file mode 100644 index 0000000..d73980d --- /dev/null +++ b/commands/send.cs @@ -0,0 +1,28 @@ +using DSharpPlus.CommandsNext; +using DSharpPlus.CommandsNext.Attributes; +using System.Threading.Tasks; + +namespace T3k3rg0.Commands +{ + public class Send : BaseCommandModule + { + [Command("sendmsg")] + [Description("Üzenetet küld ! sendmsg <üzenet>")] + [HasAdminRole] + public async Task SendAsync(CommandContext ctx, string channelParam, [RemainingText] string message) + { + // Használjuk a ChannelHelper-t a csatorna kezeléséhez + var channel = await IdExtractor.GetChannelAsync(ctx, channelParam); + + if (channel == null) + { + // Ha a csatorna érvénytelen, a ChannelHelper már válaszolt, itt nem kell további kód. + return; + } + + // Üzenet küldése a megtalált csatornára + await channel.SendMessageAsync(message); + } + } +} + diff --git a/idextractor.cs b/idextractor.cs new file mode 100644 index 0000000..625f82c --- /dev/null +++ b/idextractor.cs @@ -0,0 +1,62 @@ +using DSharpPlus.CommandsNext; +using DSharpPlus.Entities; +using System.Threading.Tasks; + +namespace T3k3rg0 +{ + class IdExtractor + { + public static async Task GetChannelAsync(CommandContext ctx, string channelParam) + { + ulong channelId; + + if (channelParam.StartsWith("https://discord.com/channels/")) + { + // Linkből szedjük ki + var parts = channelParam.Split('/'); + if (parts.Length >= 4 && ulong.TryParse(parts[4], out ulong parsedChannelId)) + { + channelId = parsedChannelId; + } + else + { + await ctx.RespondAsync("Érvénytelen csatorna link."); + return null; + } + } + else if (channelParam.StartsWith("<#") && channelParam.EndsWith(">")) + { + // Mentionből szedjük ki + var idPart = channelParam.Trim('<', '#', '>'); + if (ulong.TryParse(idPart, out ulong parsedMentionId)) + { + channelId = parsedMentionId; + } + else + { + await ctx.RespondAsync("Érvénytelen csatorna mention."); + return null; + } + } + else if (ulong.TryParse(channelParam, out ulong parsedId)) + { + // Simán ID + channelId = parsedId; + } + else + { + await ctx.RespondAsync("Érvénytelen csatorna azonosító, mention vagy link."); + return null; + } + + var channel = await ctx.Client.GetChannelAsync(channelId); + if (channel == null) + { + await ctx.RespondAsync("Nem találom a csatornát."); + return null; + } + + return channel; + } + } +} diff --git a/program.cs b/program.cs index 955e051..a464aad 100644 --- a/program.cs +++ b/program.cs @@ -11,7 +11,7 @@ namespace T3k3rg0 { static async Task Main(string[] args) { - Env.Load(); // .env fájl betöltése + Env.Load(); var loggerFactory = Logger.CreateLoggerFactory(); @@ -30,7 +30,8 @@ namespace T3k3rg0 EnableMentionPrefix = true }); - commands.RegisterCommands(); + commands.RegisterCommands(); + commands.RegisterCommands(); ReactionRoleHandler.Register(discord); await discord.ConnectAsync();