react command added
This commit is contained in:
parent
29de185cba
commit
8274a22f1b
8 changed files with 180 additions and 76 deletions
|
|
@ -3,8 +3,10 @@ using DSharpPlus.CommandsNext.Attributes;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
public class HasAdminRole : CheckBaseAttribute
|
namespace T3k3rg0.Commands
|
||||||
{
|
{
|
||||||
|
public class HasAdminRole : CheckBaseAttribute
|
||||||
|
{
|
||||||
public override async Task<bool> ExecuteCheckAsync(CommandContext ctx, bool help)
|
public override async Task<bool> ExecuteCheckAsync(CommandContext ctx, bool help)
|
||||||
{
|
{
|
||||||
bool hasAdminRole = ctx.Member?.Roles.Any(role => role.Name == "Admin") ?? false;
|
bool hasAdminRole = ctx.Member?.Roles.Any(role => role.Name == "Admin") ?? false;
|
||||||
|
|
@ -14,5 +16,5 @@ public class HasAdminRole : CheckBaseAttribute
|
||||||
}
|
}
|
||||||
return (hasAdminRole);
|
return (hasAdminRole);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
36
commands/react.cs
Normal file
36
commands/react.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
using DSharpPlus.CommandsNext;
|
||||||
|
using DSharpPlus.CommandsNext.Attributes;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using T3k3rg0.Helpers;
|
||||||
|
|
||||||
|
namespace T3k3rg0.Commands
|
||||||
|
{
|
||||||
|
public class React : BaseCommandModule
|
||||||
|
{
|
||||||
|
[Command("react")]
|
||||||
|
[Description("Reagál egy üzenetre ! react <csatorna> <üzenet> <emoji>")]
|
||||||
|
[HasAdminRole]
|
||||||
|
public async Task ReactToMessage(CommandContext ctx, string channelInput, string messageInput, string emojiInput)
|
||||||
|
{
|
||||||
|
var channel = await IdExtractor.ExtractChannelId(channelInput, ctx.Client);
|
||||||
|
if (channel == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var message = await IdExtractor.ExtractMessageId(messageInput, channel);
|
||||||
|
if (message == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var emoji = await EmojiExtractor.ExtractAsync(emojiInput, ctx.Client);
|
||||||
|
if (emoji == null)
|
||||||
|
{
|
||||||
|
await ctx.RespondAsync("Nem sikerült felismerni az emojit.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await message.CreateReactionAsync(emoji);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,26 +1,24 @@
|
||||||
using DSharpPlus.CommandsNext;
|
using DSharpPlus.CommandsNext;
|
||||||
using DSharpPlus.CommandsNext.Attributes;
|
using DSharpPlus.CommandsNext.Attributes;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using T3k3rg0.Helpers;
|
||||||
|
|
||||||
namespace T3k3rg0.Commands
|
namespace T3k3rg0.Commands
|
||||||
{
|
{
|
||||||
public class Send : BaseCommandModule
|
public class Send : BaseCommandModule
|
||||||
{
|
{
|
||||||
[Command("sendmsg")]
|
[Command("send")]
|
||||||
[Description("Üzenetet küld ! sendmsg <csatorna> <üzenet>")]
|
[Description("Üzenetet küld ! send <csatorna> <szöveg>")]
|
||||||
[HasAdminRole]
|
[HasAdminRole]
|
||||||
public async Task SendAsync(CommandContext ctx, string channelParam, [RemainingText] string message)
|
public async Task SendAsync(CommandContext ctx, string channelInput, [RemainingText] string message)
|
||||||
{
|
{
|
||||||
// Használjuk a ChannelHelper-t a csatorna kezeléséhez
|
var channel = await IdExtractor.ExtractChannelId(channelInput, ctx.Client);
|
||||||
var channel = await IdExtractor.GetChannelAsync(ctx, channelParam);
|
|
||||||
|
|
||||||
if (channel == null)
|
if (channel == null)
|
||||||
{
|
{
|
||||||
// Ha a csatorna érvénytelen, a ChannelHelper már válaszolt, itt nem kell további kód.
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Üzenet küldése a megtalált csatornára
|
|
||||||
await channel.SendMessageAsync(message);
|
await channel.SendMessageAsync(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
48
helpers/emojiextractor.cs
Normal file
48
helpers/emojiextractor.cs
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
using DSharpPlus;
|
||||||
|
using DSharpPlus.Entities;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace T3k3rg0.Helpers
|
||||||
|
{
|
||||||
|
public static class EmojiExtractor
|
||||||
|
{
|
||||||
|
public static async Task<DiscordEmoji?> ExtractAsync(string emojInput, DiscordClient client)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var unicodeEmoji = DiscordEmoji.FromUnicode(emojInput);
|
||||||
|
return unicodeEmoji;
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var namedEmoji = DiscordEmoji.FromName(client, emojInput);
|
||||||
|
return namedEmoji;
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ulong emojiId;
|
||||||
|
|
||||||
|
// Ha teljes emojiformátumban van (pl. <:nev:123456789012345678>)
|
||||||
|
var match = System.Text.RegularExpressions.Regex.Match(emojInput, @"<a?:\w+:(\d{17,20})>");
|
||||||
|
if (match.Success && ulong.TryParse(match.Groups[1].Value, out emojiId))
|
||||||
|
{
|
||||||
|
return DiscordEmoji.FromGuildEmote(client, emojiId);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ha csak simán az ID-t írta be
|
||||||
|
if (ulong.TryParse(emojInput, out emojiId))
|
||||||
|
{
|
||||||
|
return DiscordEmoji.FromGuildEmote(client, emojiId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
81
helpers/idextractor.cs
Normal file
81
helpers/idextractor.cs
Normal file
|
|
@ -0,0 +1,81 @@
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using DSharpPlus;
|
||||||
|
using DSharpPlus.Entities;
|
||||||
|
|
||||||
|
namespace T3k3rg0.Helpers
|
||||||
|
{
|
||||||
|
public static class IdExtractor
|
||||||
|
{
|
||||||
|
public static ulong? ExtractChannel(string channelInput)
|
||||||
|
{
|
||||||
|
if (channelInput.StartsWith("https://discord.com/channels/"))
|
||||||
|
{
|
||||||
|
var parts = channelInput.Split('/');
|
||||||
|
if (parts.Length >= 6 && ulong.TryParse(parts[5], out ulong parsedChannelLinkId))
|
||||||
|
return parsedChannelLinkId;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (channelInput.StartsWith("<#") && channelInput.EndsWith(">"))
|
||||||
|
{
|
||||||
|
var trimmed = channelInput.Trim('<', '#', '>');
|
||||||
|
if (ulong.TryParse(trimmed, out ulong parsedChannelMentionId))
|
||||||
|
return parsedChannelMentionId;
|
||||||
|
}
|
||||||
|
else if (ulong.TryParse(channelInput, out ulong parsedRawCannelId))
|
||||||
|
{
|
||||||
|
return parsedRawCannelId;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<DiscordChannel?> ExtractChannelId(string channelInput, DiscordClient client)
|
||||||
|
{
|
||||||
|
var channelId = ExtractChannel(channelInput);
|
||||||
|
if (channelId is null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var channel = await client.GetChannelAsync(channelId.Value);
|
||||||
|
return channel;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static ulong? ExtractMessage(string messageInput)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (messageInput.StartsWith("https://discord.com/channels/"))
|
||||||
|
{
|
||||||
|
var parts = messageInput.Split('/');
|
||||||
|
if (parts.Length >= 7 && ulong.TryParse(parts[6], out ulong parsedMessageLinkId))
|
||||||
|
return parsedMessageLinkId;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (ulong.TryParse(messageInput, out ulong parsedRawMessageId))
|
||||||
|
{
|
||||||
|
return parsedRawMessageId;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<DiscordMessage?> ExtractMessageId(string messageInput, DiscordChannel channel)
|
||||||
|
{
|
||||||
|
var messageId = ExtractMessage(messageInput);
|
||||||
|
if (messageId is null)
|
||||||
|
return null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var message = await channel.GetMessageAsync(messageId.Value);
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,62 +0,0 @@
|
||||||
using DSharpPlus.CommandsNext;
|
|
||||||
using DSharpPlus.Entities;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace T3k3rg0
|
|
||||||
{
|
|
||||||
class IdExtractor
|
|
||||||
{
|
|
||||||
public static async Task<DiscordChannel> 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -32,6 +32,7 @@ namespace T3k3rg0
|
||||||
|
|
||||||
commands.RegisterCommands<T3k3rg0.Commands.Hello>();
|
commands.RegisterCommands<T3k3rg0.Commands.Hello>();
|
||||||
commands.RegisterCommands<T3k3rg0.Commands.Send>();
|
commands.RegisterCommands<T3k3rg0.Commands.Send>();
|
||||||
|
commands.RegisterCommands<T3k3rg0.Commands.React>();
|
||||||
ReactionRoleHandler.Register(discord);
|
ReactionRoleHandler.Register(discord);
|
||||||
|
|
||||||
await discord.ConnectAsync();
|
await discord.ConnectAsync();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue