HOLY SHIT IT WORKS!
This commit is contained in:
@@ -7,13 +7,12 @@ namespace Reactor.Commands
|
|||||||
public static class AddCommand
|
public static class AddCommand
|
||||||
{
|
{
|
||||||
public static async Task Execute(
|
public static async Task Execute(
|
||||||
|
ValourClient client,
|
||||||
Dictionary<long, Channel> channelCache,
|
Dictionary<long, Channel> channelCache,
|
||||||
long channelId,
|
long channelId,
|
||||||
long messageId,
|
long messageId,
|
||||||
string emoji,
|
string emoji,
|
||||||
long roleId,
|
long roleId)
|
||||||
ValourClient client,
|
|
||||||
Planet planet)
|
|
||||||
{
|
{
|
||||||
//Check if the current channel is in the cache (should never happen but you never know!)
|
//Check if the current channel is in the cache (should never happen but you never know!)
|
||||||
if (!channelCache.TryGetValue(channelId, out var channel))
|
if (!channelCache.TryGetValue(channelId, out var channel))
|
||||||
@@ -46,6 +45,8 @@ namespace Reactor.Commands
|
|||||||
//Add reaction-role mapping to DB and Cache
|
//Add reaction-role mapping to DB and Cache
|
||||||
await ReactionRoleService.AddReactionAsync(messageId, emoji, roleId);
|
await ReactionRoleService.AddReactionAsync(messageId, emoji, roleId);
|
||||||
|
|
||||||
|
ReactionRoleService.SubscribeToMessageReactions(client, channelCache, message);
|
||||||
|
|
||||||
await channel.SendMessageAsync($"Added reaction {emoji} -> role {roleId} for message {messageId}");
|
await channel.SendMessageAsync($"Added reaction {emoji} -> role {roleId} for message {messageId}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,12 +61,6 @@ namespace Reactor.Commands
|
|||||||
Reactions = new Dictionary<string, long>()
|
Reactions = new Dictionary<string, long>()
|
||||||
};
|
};
|
||||||
|
|
||||||
//Subscribe events
|
|
||||||
// sentMessage.ReactionAdded += async () =>
|
|
||||||
// {
|
|
||||||
// await ReactionRoleService.HandleReactionAddedAsync(channelCache, sentMessage);
|
|
||||||
// };
|
|
||||||
|
|
||||||
Console.WriteLine($"Created reaction message {sentMessage.Id} in channel {channelId}");
|
Console.WriteLine($"Created reaction message {sentMessage.Id} in channel {channelId}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
47
Commands/DeleteCommand.cs
Normal file
47
Commands/DeleteCommand.cs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
using Reactor.Services;
|
||||||
|
using Valour.Sdk.Client;
|
||||||
|
using Valour.Sdk.Models;
|
||||||
|
|
||||||
|
namespace Reactor.Commands
|
||||||
|
{
|
||||||
|
public static class DeleteCommand
|
||||||
|
{
|
||||||
|
public static async Task Execute(
|
||||||
|
ValourClient client,
|
||||||
|
Dictionary<long, Channel> channelCache,
|
||||||
|
long channelId,
|
||||||
|
long messageId)
|
||||||
|
{
|
||||||
|
//Check if channel in cache
|
||||||
|
if (!channelCache.TryGetValue(channelId, out var channel))
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Channel {channelId} not found in cache.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Check if message is actually a reaction message
|
||||||
|
if (!ReactionRoleService.Messages.TryGetValue(messageId, out var reactionMsg))
|
||||||
|
{
|
||||||
|
await channel.SendMessageAsync($"Message ID {messageId} is not tracked as a reaction message.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Delete the actual message
|
||||||
|
var recentMessages = await channel.GetLastMessagesAsync(50);
|
||||||
|
var message = recentMessages.FirstOrDefault(m => m.Id == messageId);
|
||||||
|
if (message != null)
|
||||||
|
{
|
||||||
|
await message.DeleteAsync();
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Message {messageId} not found in recent messages, skipping deletion of message.");
|
||||||
|
}
|
||||||
|
|
||||||
|
//Remove from cache and database
|
||||||
|
await ReactionRoleService.RemoveMessageAsync(messageId);
|
||||||
|
ReactionRoleService.ResetSubscription(messageId);
|
||||||
|
|
||||||
|
await channel.SendMessageAsync($"Deleted reaction message {messageId} and all its role mappings.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,6 +10,9 @@ public static class HelpCommand
|
|||||||
- `{prefix}help` - Shows this list.
|
- `{prefix}help` - Shows this list.
|
||||||
- `{prefix}source` - Shows my source code!
|
- `{prefix}source` - Shows my source code!
|
||||||
- `{prefix}create` - Creates the Reaction Message.
|
- `{prefix}create` - Creates the Reaction Message.
|
||||||
|
- `{prefix}delete` - Deletes a Reaction Message.
|
||||||
|
- `{prefix}add` - Adds a Reaction Role to a Valid Message.
|
||||||
|
- `{prefix}remove` - Removes a Reaction Role from a Valid Message.
|
||||||
";
|
";
|
||||||
|
|
||||||
if (channelCache.TryGetValue(channelId, out var channel))
|
if (channelCache.TryGetValue(channelId, out var channel))
|
||||||
|
|||||||
50
Commands/RemoveCommand.cs
Normal file
50
Commands/RemoveCommand.cs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
using Reactor.Services;
|
||||||
|
using Valour.Sdk.Client;
|
||||||
|
using Valour.Sdk.Models;
|
||||||
|
|
||||||
|
namespace Reactor.Commands
|
||||||
|
{
|
||||||
|
public static class RemoveCommand
|
||||||
|
{
|
||||||
|
public static async Task Execute(
|
||||||
|
ValourClient client,
|
||||||
|
Dictionary<long, Channel> channelCache,
|
||||||
|
long channelId,
|
||||||
|
long messageId,
|
||||||
|
string emoji)
|
||||||
|
{
|
||||||
|
//Check if channel in cache
|
||||||
|
if (!channelCache.TryGetValue(channelId, out var channel))
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Channel {channelId} not found in cache.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Check if message is actually a reaction message
|
||||||
|
if (!ReactionRoleService.Messages.TryGetValue(messageId, out var reactionMsg))
|
||||||
|
{
|
||||||
|
await channel.SendMessageAsync($"Message ID {messageId} is not tracked as a reaction message.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Check if the emoji is actually a valid reaction on the message
|
||||||
|
if (!reactionMsg.Reactions.ContainsKey(emoji))
|
||||||
|
{
|
||||||
|
await channel.SendMessageAsync($"Emoji {emoji} is not mapped to any role on message {messageId}.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Fetch the message and remove the reaction
|
||||||
|
var recentMessages = await channel.GetLastMessagesAsync(50);
|
||||||
|
var message = recentMessages.FirstOrDefault(m => m.Id == messageId);
|
||||||
|
if (message != null)
|
||||||
|
{
|
||||||
|
await message.RemoveReactionAsync(emoji);
|
||||||
|
}
|
||||||
|
|
||||||
|
await ReactionRoleService.RemoveReactionAsync(messageId, emoji);
|
||||||
|
|
||||||
|
await channel.SendMessageAsync($"Removed reaction {emoji} from message {messageId}.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using System.ComponentModel;
|
||||||
using Valour.Sdk.Client;
|
using Valour.Sdk.Client;
|
||||||
using Valour.Sdk.Models;
|
using Valour.Sdk.Models;
|
||||||
|
|
||||||
@@ -30,7 +31,7 @@ namespace Reactor.Services
|
|||||||
|
|
||||||
//Initialize the Database
|
//Initialize the Database
|
||||||
await DatabaseService.InitializeAsync();
|
await DatabaseService.InitializeAsync();
|
||||||
await ReactionRoleService.LoadAllAsync();
|
await ReactionRoleService.LoadAllAsync(client);
|
||||||
Console.WriteLine($"Loaded {ReactionRoleService.Messages.Count} reaction messages into memory.");
|
Console.WriteLine($"Loaded {ReactionRoleService.Messages.Count} reaction messages into memory.");
|
||||||
|
|
||||||
//Initialize the Planets
|
//Initialize the Planets
|
||||||
@@ -40,8 +41,52 @@ namespace Reactor.Services
|
|||||||
await PlanetService.InitializePlanetsAsync(client, channelCache, initializedPlanets);
|
await PlanetService.InitializePlanetsAsync(client, channelCache, initializedPlanets);
|
||||||
};
|
};
|
||||||
|
|
||||||
//Initialize the Messages
|
//Fucking pain in my ass is what this is, i dont even wanna comment on it
|
||||||
client.MessageService.MessageReceived += async (msg) => await MessageService.HandleMessageAsync(client, channelCache, msg, prefix);
|
foreach (var reactionMessage in ReactionRoleService.Messages.Values.ToList())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if(!channelCache.TryGetValue(reactionMessage.ChannelId, out var channel))
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Channel {reactionMessage.ChannelId} not found, pruning message {reactionMessage.MessageId}.");
|
||||||
|
await ReactionRoleService.RemoveMessageAsync(reactionMessage.MessageId);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var messages = await channel.GetMessagesAsync(reactionMessage.MessageId + 1, 50);
|
||||||
|
Console.WriteLine($"Fetched {messages?.Count ?? 0} messages from channel {reactionMessage.ChannelId}");
|
||||||
|
var match = messages?.FirstOrDefault(m => m.Id == reactionMessage.MessageId);
|
||||||
|
|
||||||
|
if (match == null)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Message {reactionMessage.MessageId} not found, pruning.");
|
||||||
|
await ReactionRoleService.RemoveMessageAsync(reactionMessage.MessageId);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
ReactionRoleService.SubscribeToMessageReactions(client, channelCache, match);
|
||||||
|
Console.WriteLine($"Subscribed to reactions for message {reactionMessage.MessageId}");
|
||||||
|
} catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error setting up message {reactionMessage.MessageId}: {ex.Message}, pruning.");
|
||||||
|
await ReactionRoleService.RemoveMessageAsync(reactionMessage.MessageId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
client.MessageService.MessageReceived += async (message) =>
|
||||||
|
{
|
||||||
|
await MessageService.HandleMessageAsync(client, channelCache, message, prefix);
|
||||||
|
};
|
||||||
|
|
||||||
|
client.MessageService.MessageDeleted += async (message) =>
|
||||||
|
{
|
||||||
|
if (ReactionRoleService.Messages.ContainsKey(message.Id))
|
||||||
|
{
|
||||||
|
await ReactionRoleService.RemoveMessageAsync(message.Id);
|
||||||
|
ReactionRoleService.ResetSubscription(message.Id);
|
||||||
|
Console.WriteLine($"Reaction message {message.Id} was deleted, removed from DB and cache.");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
//Bot is active and ready
|
//Bot is active and ready
|
||||||
Console.WriteLine("Bot ready and listening...");
|
Console.WriteLine("Bot ready and listening...");
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using Reactor.Commands;
|
using Reactor.Commands;
|
||||||
using Valour.Sdk.Client;
|
using Valour.Sdk.Client;
|
||||||
using Valour.Sdk.Models;
|
using Valour.Sdk.Models;
|
||||||
|
using Valour.Shared.Authorization;
|
||||||
|
|
||||||
namespace Reactor.Services
|
namespace Reactor.Services
|
||||||
{
|
{
|
||||||
@@ -24,6 +25,8 @@ namespace Reactor.Services
|
|||||||
var member = await message.FetchAuthorMemberAsync();
|
var member = await message.FetchAuthorMemberAsync();
|
||||||
string memberPing = member != null ? $"«@m-{member.Id}»" : "";
|
string memberPing = member != null ? $"«@m-{member.Id}»" : "";
|
||||||
|
|
||||||
|
bool hasPermission = await HasPermissionAsync(member, channelCache[channelId]);
|
||||||
|
|
||||||
string withoutPrefix = content.Substring(prefix.Length);
|
string withoutPrefix = content.Substring(prefix.Length);
|
||||||
|
|
||||||
var parts = withoutPrefix.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
var parts = withoutPrefix.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||||||
@@ -44,6 +47,13 @@ namespace Reactor.Services
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case "create":
|
case "create":
|
||||||
|
|
||||||
|
if (!hasPermission)
|
||||||
|
{
|
||||||
|
await channelCache[channelId].SendMessageAsync($"{memberPing} You need Manage Roles or Full Control to use this command.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (parts.Length < 2)
|
if (parts.Length < 2)
|
||||||
{
|
{
|
||||||
await channelCache[channelId].SendMessageAsync($"{memberPing} Usage: {prefix}create <default message text>");
|
await channelCache[channelId].SendMessageAsync($"{memberPing} Usage: {prefix}create <default message text>");
|
||||||
@@ -57,10 +67,40 @@ namespace Reactor.Services
|
|||||||
}
|
}
|
||||||
|
|
||||||
var messageText = string.Join(' ', parts[1..]);
|
var messageText = string.Join(' ', parts[1..]);
|
||||||
await CreateCommand.Execute(channelCache, channelId, messageText, message.PlanetId.Value);
|
await CreateCommand.Execute(client, channelCache, channelId, messageText, message.PlanetId.Value);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "delete":
|
||||||
|
|
||||||
|
if (!hasPermission)
|
||||||
|
{
|
||||||
|
await channelCache[channelId].SendMessageAsync($"{memberPing} You need Manage Roles or Full Control to use this command.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parts.Length < 2)
|
||||||
|
{
|
||||||
|
await channelCache[channelId].SendMessageAsync($"{memberPing} Usage: {prefix}delete <messageId>");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!long.TryParse(parts[1], out var deleteMsgId))
|
||||||
|
{
|
||||||
|
await channelCache[channelId].SendMessageAsync($"{memberPing} Invalid message ID.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await DeleteCommand.Execute(client, channelCache, channelId, deleteMsgId);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "add":
|
case "add":
|
||||||
|
|
||||||
|
if (!hasPermission)
|
||||||
|
{
|
||||||
|
await channelCache[channelId].SendMessageAsync($"{memberPing} You need Manage Roles or Full Control to use this command.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (parts.Length < 4)
|
if (parts.Length < 4)
|
||||||
{
|
{
|
||||||
await channelCache[channelId].SendMessageAsync($"{memberPing} Usage: {prefix}add <messageId> <emoji> <roleId>");
|
await channelCache[channelId].SendMessageAsync($"{memberPing} Usage: {prefix}add <messageId> <emoji> <roleId>");
|
||||||
@@ -81,9 +121,41 @@ namespace Reactor.Services
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await AddCommand.Execute(channelCache, channelId, msgId, emoji, roleId, client, message.Planet);
|
await AddCommand.Execute(client, channelCache, channelId, msgId, emoji, roleId);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "remove":
|
||||||
|
|
||||||
|
if (!hasPermission)
|
||||||
|
{
|
||||||
|
await channelCache[channelId].SendMessageAsync($"{memberPing} You need Manage Roles or Full Control to use this command.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parts.Length < 3)
|
||||||
|
{
|
||||||
|
await channelCache[channelId].SendMessageAsync($"{memberPing} Usage: {prefix}remove <messageId> <emoji>");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!long.TryParse(parts[1], out var removeMsgId))
|
||||||
|
{
|
||||||
|
await channelCache[channelId].SendMessageAsync($"{memberPing} Invalid message ID.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var removeEmoji = parts[2];
|
||||||
|
await RemoveCommand.Execute(client, channelCache, channelId, removeMsgId, removeEmoji);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static async Task<bool> HasPermissionAsync(PlanetMember member, Channel channel)
|
||||||
|
{
|
||||||
|
if (member == null) return false;
|
||||||
|
|
||||||
|
return member.HasPermission(PlanetPermissions.FullControl) ||
|
||||||
|
member.HasPermission(PlanetPermissions.ManageRoles);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -13,7 +13,7 @@ namespace Reactor.Services
|
|||||||
public static Dictionary<long, ReactionMessage> Messages { get; private set; } = new();
|
public static Dictionary<long, ReactionMessage> Messages { get; private set; } = new();
|
||||||
|
|
||||||
//Load all messages and reaction role mappings
|
//Load all messages and reaction role mappings
|
||||||
public static async Task LoadAllAsync()
|
public static async Task LoadAllAsync(ValourClient client)
|
||||||
{
|
{
|
||||||
Messages.Clear();
|
Messages.Clear();
|
||||||
|
|
||||||
@@ -80,9 +80,85 @@ namespace Reactor.Services
|
|||||||
msg.Reactions[emoji] = roleId;
|
msg.Reactions[emoji] = roleId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task HandleReactionAddedAsync(
|
public static async Task RemoveReactionAsync(long messageId, string emoji)
|
||||||
|
{
|
||||||
|
if (!Messages.TryGetValue(messageId, out var msg))
|
||||||
|
return;
|
||||||
|
|
||||||
|
using var connection = new SqliteConnection(_connectionString);
|
||||||
|
await connection.OpenAsync();
|
||||||
|
|
||||||
|
var cmd = connection.CreateCommand();
|
||||||
|
cmd.CommandText = "DELETE FROM ReactionRoles WHERE ReactionMessageId = @msgId AND Emoji = @emoji";
|
||||||
|
cmd.Parameters.AddWithValue("@msgId", msg.Id);
|
||||||
|
cmd.Parameters.AddWithValue("@emoji", emoji);
|
||||||
|
await cmd.ExecuteNonQueryAsync();
|
||||||
|
|
||||||
|
msg.Reactions.Remove(emoji);
|
||||||
|
Console.WriteLine($"Removed reaction {emoji} from message {messageId}.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static readonly HashSet<long> _subscribedMessages = new();
|
||||||
|
|
||||||
|
public static void SubscribeToMessageReactions(
|
||||||
|
ValourClient client,
|
||||||
Dictionary<long, Channel> channelCache,
|
Dictionary<long, Channel> channelCache,
|
||||||
Message message)
|
Message syncedMessage)
|
||||||
|
{
|
||||||
|
if (!_subscribedMessages.Add(syncedMessage.Id))
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Already subscribed to message {syncedMessage.Id}, skipping.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Action<MessageReaction> addhandler = (reaction) =>
|
||||||
|
{
|
||||||
|
_ = Task.Run(async () =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Reaction added: {reaction.Emoji} by {reaction.AuthorUserId}");
|
||||||
|
await HandleReactionAddedAsync(client, channelCache, syncedMessage, reaction);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error in addhandler: {ex.Message}");
|
||||||
|
Console.WriteLine(ex.StackTrace);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Action<MessageReaction> removehandler = (reaction) =>
|
||||||
|
{
|
||||||
|
_ = Task.Run(async () =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Reaction removed: {reaction.Emoji} by {reaction.AuthorUserId}");
|
||||||
|
await HandleReactionRemovedAsync(client, channelCache, syncedMessage, reaction);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error in removehandler: {ex.Message}");
|
||||||
|
Console.WriteLine(ex.StackTrace);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
syncedMessage.ReactionAdded += addhandler;
|
||||||
|
syncedMessage.ReactionRemoved += removehandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void ResetSubscription(long messageId)
|
||||||
|
{
|
||||||
|
_subscribedMessages.Remove(messageId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task HandleReactionAddedAsync(
|
||||||
|
ValourClient client,
|
||||||
|
Dictionary<long, Channel> channelCache,
|
||||||
|
Message message,
|
||||||
|
MessageReaction reaction)
|
||||||
{
|
{
|
||||||
if (!Messages.TryGetValue(message.Id, out var cachedMsg))
|
if (!Messages.TryGetValue(message.Id, out var cachedMsg))
|
||||||
return;
|
return;
|
||||||
@@ -90,85 +166,100 @@ namespace Reactor.Services
|
|||||||
if (!channelCache.TryGetValue(cachedMsg.ChannelId, out var channel))
|
if (!channelCache.TryGetValue(cachedMsg.ChannelId, out var channel))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
foreach (var kvp in message.Reactions)
|
if (!cachedMsg.Reactions.TryGetValue(reaction.Emoji, out var roleId))
|
||||||
{
|
return;
|
||||||
string emoji = kvp.Emoji;
|
|
||||||
if (!cachedMsg.Reactions.TryGetValue(emoji, out var roleId))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
//Fetch role name
|
|
||||||
var role = channel.Planet.Roles.FirstOrDefault(r => r.Id == roleId);
|
var role = channel.Planet.Roles.FirstOrDefault(r => r.Id == roleId);
|
||||||
string roleName = role != null ? role.Name : $"Role {roleId}";
|
string roleName = role != null ? role.Name : $"Role {roleId}";
|
||||||
|
|
||||||
//Fetch member
|
var member = await channel.Planet.FetchMemberByUserAsync(reaction.AuthorUserId);
|
||||||
var member = await channel.Planet.FetchMemberAsync(kvp.AuthorUserId);
|
|
||||||
if (member == null) return;
|
if (member == null) return;
|
||||||
|
|
||||||
//Apply role to user
|
// Check if member already has the role
|
||||||
|
if (member.Roles.Any(r => r.Id == roleId))
|
||||||
|
{
|
||||||
|
Console.WriteLine($"User {reaction.AuthorUserId} already has role {roleId}, skipping.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
await member.AddRoleAsync(roleId);
|
await member.AddRoleAsync(roleId);
|
||||||
|
|
||||||
//Confirmation
|
var confirm = await channel.SendMessageAsync($"«@m-{member.Id}» has been added to the role {roleName}");
|
||||||
var confirm = await channel.SendMessageAsync($"«@m-{member.Id}» has been given the role {roleName}");
|
if (confirm.Success && confirm.Data != null)
|
||||||
|
{
|
||||||
await Task.Delay(cachedMsg.DeleteDelaySeconds * 1000);
|
await Task.Delay(cachedMsg.DeleteDelaySeconds * 1000);
|
||||||
await confirm.Data.DeleteAsync();
|
if (client.Cache.Messages.TryGet(confirm.Data.Id, out var cachedConfirm))
|
||||||
|
{
|
||||||
|
await cachedConfirm.DeleteAsync();
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Could not find confirmation message {confirm.Data.Id} in cache.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// public static async Task HandleReactionAddedAsync(
|
public static async Task HandleReactionRemovedAsync(
|
||||||
// ValourClient client,
|
ValourClient client,
|
||||||
// Dictionary<long, Channel> channelCache,
|
Dictionary<long, Channel> channelCache,
|
||||||
// MessageReaction reaction)
|
Message message,
|
||||||
// {
|
MessageReaction reaction)
|
||||||
// if (!Messages.TryGetValue(reaction.MessageId, out var msg))
|
{
|
||||||
// return;
|
if (!Messages.TryGetValue(message.Id, out var cachedMsg))
|
||||||
|
return;
|
||||||
|
|
||||||
// if (!msg.Reactions.TryGetValue(reaction.Emoji, out var roleId))
|
if (!channelCache.TryGetValue(cachedMsg.ChannelId, out var channel))
|
||||||
// return;
|
return;
|
||||||
|
|
||||||
// if (!channelCache.TryGetValue(msg.ChannelId, out var channel))
|
if (!cachedMsg.Reactions.TryGetValue(reaction.Emoji, out var roleId))
|
||||||
// return;
|
return;
|
||||||
|
|
||||||
// var role = channel.Planet.Roles.FirstOrDefault(r => r.Id == roleId);
|
var role = channel.Planet.Roles.FirstOrDefault(r => r.Id == roleId);
|
||||||
// string roleName = role != null ? role.Name : $"Role {roleId}";
|
string roleName = role != null ? role.Name : $"Role {roleId}";
|
||||||
|
|
||||||
// //Fetch the member
|
var member = await channel.Planet.FetchMemberByUserAsync(reaction.AuthorUserId);
|
||||||
// var member = await channel.Planet.FetchMemberAsync(reaction.AuthorUserId);
|
if (member == null) return;
|
||||||
// if (member == null) return;
|
|
||||||
|
|
||||||
// //Add role
|
// Check if member actually has the role before removing
|
||||||
// await member.AddRoleAsync(roleId);
|
if (!member.Roles.Any(r => r.Id == roleId))
|
||||||
|
{
|
||||||
|
Console.WriteLine($"User {reaction.AuthorUserId} does not have role {roleId}, skipping.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// //Confirmation
|
await member.RemoveRoleAsync(roleId);
|
||||||
// var confirm = await channel.SendMessageAsync($"«@m-{member.Id}» has been given the role {roleName}");
|
|
||||||
// await Task.Delay(msg.DeleteDelaySeconds * 1000);
|
|
||||||
// await confirm.Data.DeleteAsync();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// public static async Task HandleReactionRemovedAsync(
|
var confirm = await channel.SendMessageAsync($"«@m-{member.Id}» has been removed from the role {roleName}");
|
||||||
// ValourClient client,
|
if (confirm.Success && confirm.Data != null)
|
||||||
// Dictionary<long, Channel> channelCache,
|
{
|
||||||
// MessageReaction reaction)
|
await Task.Delay(cachedMsg.DeleteDelaySeconds * 1000);
|
||||||
// {
|
if (client.Cache.Messages.TryGet(confirm.Data.Id, out var cachedConfirm))
|
||||||
// if (!Messages.TryGetValue(reaction.MessageId, out var msg))
|
{
|
||||||
// return;
|
await cachedConfirm.DeleteAsync();
|
||||||
|
} else
|
||||||
// if (!msg.Reactions.TryGetValue(reaction.Emoji, out var roleId))
|
{
|
||||||
// return;
|
Console.WriteLine($"Could not find confirmation message {confirm.Data.Id} in cache.");
|
||||||
|
}
|
||||||
// if (!channelCache.TryGetValue(msg.ChannelId, out var channel))
|
}
|
||||||
// return;
|
}
|
||||||
|
|
||||||
// var role = channel.Planet.Roles.FirstOrDefault(r => r.Id == roleId);
|
public static async Task RemoveMessageAsync(long messageId)
|
||||||
// string roleName = role != null ? role.Name : $"role {roleId}";
|
{
|
||||||
|
if (!Messages.TryGetValue(messageId, out var msg)) return;
|
||||||
// var member = await channel.Planet.FetchMemberAsync(reaction.AuthorUserId);
|
|
||||||
// if (member == null) return;
|
using var connection = new SqliteConnection(_connectionString);
|
||||||
|
await connection.OpenAsync();
|
||||||
// await member.RemoveRoleAsync(roleId);
|
|
||||||
|
var cmd = connection.CreateCommand();
|
||||||
// var confirm = await channel.SendMessageAsync($"«@m-{member.Id}» has been removed from the role {roleName}");
|
cmd.CommandText = @"
|
||||||
// await Task.Delay(msg.DeleteDelaySeconds * 1000);
|
DELETE FROM ReactionRoles WHERE ReactionMessageId = @id;
|
||||||
// await confirm.Data.DeleteAsync();
|
DELETE FROM ReactionMessages WHERE MessageId = @messageId;
|
||||||
// }
|
";
|
||||||
|
cmd.Parameters.AddWithValue("@id", msg.Id);
|
||||||
|
cmd.Parameters.AddWithValue("@messageId", messageId);
|
||||||
|
await cmd.ExecuteNonQueryAsync();
|
||||||
|
|
||||||
|
Messages.Remove(messageId);
|
||||||
|
Console.WriteLine($"Removed stale reaction message {messageId} from DB and memory.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user