added reactionrole manager
This commit is contained in:
parent
8274a22f1b
commit
1695770f9f
4 changed files with 133 additions and 1 deletions
|
|
@ -27,7 +27,6 @@ namespace T3k3rg0.Commands
|
|||
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);
|
||||
|
|
|
|||
67
commands/reactionrolemanager.cs
Normal file
67
commands/reactionrolemanager.cs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
using DSharpPlus.CommandsNext;
|
||||
using DSharpPlus.CommandsNext.Attributes;
|
||||
using DSharpPlus.Entities;
|
||||
using System.Threading.Tasks;
|
||||
using T3k3rg0.Helpers;
|
||||
|
||||
namespace T3k3rg0.Commands
|
||||
{
|
||||
public class ReactionRoleManager : BaseCommandModule
|
||||
{
|
||||
[Command("addreactionrole")]
|
||||
[Description("Hozzáad egy új reaction role-t ! reactionrole <csatorna> <üzenet> <emoji> <@szerep>")]
|
||||
[HasAdminRole]
|
||||
public async Task AddReactionRole(CommandContext ctx, string channelInput, string messageInput, string emojiInput, DiscordRole role)
|
||||
{
|
||||
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)
|
||||
return;
|
||||
|
||||
bool inserted = ReactionRoleStorage.TryAddReactionRole(message.Id, emoji.ToString(), role.Id);
|
||||
if (!inserted)
|
||||
return;
|
||||
|
||||
await message.CreateReactionAsync(emoji);
|
||||
}
|
||||
[Command("removereactionrole")]
|
||||
[Description("Eltávolít egy meglévő reaction role-t ! removereactionrole <csatorna> <üzenet> <emoji>")]
|
||||
[HasAdminRole]
|
||||
public async Task RemoveReactionRole(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)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bool removed = ReactionRoleStorage.TryRemoveReactionRole(message.Id, emoji.ToString());
|
||||
if (!removed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await message.DeleteOwnReactionAsync(emoji);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
65
helpers/reactionrolestorage.cs
Normal file
65
helpers/reactionrolestorage.cs
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
using Microsoft.Data.Sqlite;
|
||||
using System;
|
||||
|
||||
namespace T3k3rg0.Helpers
|
||||
{
|
||||
public static class ReactionRoleStorage
|
||||
{
|
||||
private static readonly string DbPath = Environment.GetEnvironmentVariable("DB_ROLES_LOC");
|
||||
|
||||
public static ulong? GetRoleId(ulong messageId, string emojiRaw)
|
||||
{
|
||||
using var conn = new SqliteConnection($"Data Source={DbPath}");
|
||||
conn.Open();
|
||||
|
||||
using var cmd = conn.CreateCommand();
|
||||
cmd.CommandText = @"
|
||||
SELECT role_id FROM reaction_roles
|
||||
WHERE message_id = @msg AND emoji = @emoji";
|
||||
cmd.Parameters.AddWithValue("@msg", (long)messageId);
|
||||
cmd.Parameters.AddWithValue("@emoji", emojiRaw);
|
||||
|
||||
using var reader = cmd.ExecuteReader();
|
||||
if (reader.Read())
|
||||
{
|
||||
return (ulong)(long)reader["role_id"];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static bool TryAddReactionRole(ulong messageId, string emojiRaw, ulong roleId)
|
||||
{
|
||||
using var conn = new SqliteConnection($"Data Source={DbPath}");
|
||||
conn.Open();
|
||||
|
||||
using var cmd = conn.CreateCommand();
|
||||
cmd.CommandText = @"
|
||||
INSERT OR IGNORE INTO reaction_roles (message_id, emoji, role_id)
|
||||
VALUES (@msg, @emoji, @role)";
|
||||
cmd.Parameters.AddWithValue("@msg", (long)messageId);
|
||||
cmd.Parameters.AddWithValue("@emoji", emojiRaw);
|
||||
cmd.Parameters.AddWithValue("@role", (long)roleId);
|
||||
|
||||
int rowsAffected = cmd.ExecuteNonQuery();
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
public static bool TryRemoveReactionRole(ulong messageId, string emojiRaw)
|
||||
{
|
||||
using var conn = new SqliteConnection($"Data Source={DbPath}");
|
||||
conn.Open();
|
||||
|
||||
using var cmd = conn.CreateCommand();
|
||||
cmd.CommandText = @"
|
||||
DELETE FROM reaction_roles
|
||||
WHERE message_id = @msg AND emoji = @emoji";
|
||||
cmd.Parameters.AddWithValue("@msg", (long)messageId);
|
||||
cmd.Parameters.AddWithValue("@emoji", emojiRaw);
|
||||
|
||||
int rowsAffected = cmd.ExecuteNonQuery();
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -33,6 +33,7 @@ namespace T3k3rg0
|
|||
commands.RegisterCommands<T3k3rg0.Commands.Hello>();
|
||||
commands.RegisterCommands<T3k3rg0.Commands.Send>();
|
||||
commands.RegisterCommands<T3k3rg0.Commands.React>();
|
||||
commands.RegisterCommands<T3k3rg0.Commands.ReactionRoleManager>();
|
||||
ReactionRoleHandler.Register(discord);
|
||||
|
||||
await discord.ConnectAsync();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue