Add a migration script for role listeners
This commit is contained in:
parent
33b17b373f
commit
46fee88f03
2 changed files with 80 additions and 1 deletions
|
@ -1,12 +1,16 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using Geekbot.net.Lib.AlmostRedis;
|
||||
using Geekbot.net.Lib.ErrorHandling;
|
||||
using Geekbot.net.Lib.GlobalSettings;
|
||||
using Geekbot.net.Lib.Logger;
|
||||
using Geekbot.net.Lib.ReactionListener;
|
||||
using Geekbot.net.Lib.UserRepository;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace Geekbot.net.Commands.Admin.Owner
|
||||
{
|
||||
|
@ -17,16 +21,22 @@ namespace Geekbot.net.Commands.Admin.Owner
|
|||
private readonly DiscordSocketClient _client;
|
||||
private readonly IErrorHandler _errorHandler;
|
||||
private readonly IGlobalSettings _globalSettings;
|
||||
private readonly IDatabase _redis;
|
||||
private readonly IReactionListener _reactionListener;
|
||||
private readonly IGeekbotLogger _logger;
|
||||
private readonly IUserRepository _userRepository;
|
||||
|
||||
public Owner(DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler, IGlobalSettings globalSettings)
|
||||
public Owner(DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler, IGlobalSettings globalSettings,
|
||||
IAlmostRedis redis, IReactionListener reactionListener
|
||||
)
|
||||
{
|
||||
_client = client;
|
||||
_logger = logger;
|
||||
_userRepository = userRepositry;
|
||||
_errorHandler = errorHandler;
|
||||
_globalSettings = globalSettings;
|
||||
_redis = redis.Db;
|
||||
_reactionListener = reactionListener;
|
||||
}
|
||||
|
||||
[Command("youtubekey", RunMode = RunMode.Async)]
|
||||
|
@ -122,5 +132,72 @@ namespace Geekbot.net.Commands.Admin.Owner
|
|||
await _errorHandler.HandleCommandException(e, Context);
|
||||
}
|
||||
}
|
||||
|
||||
[Command("migrate_listeners", RunMode = RunMode.Async)]
|
||||
public async Task MigrateListeners()
|
||||
{
|
||||
try
|
||||
{
|
||||
var messageIds = _redis.SetMembers("MessageIds");
|
||||
var connectedGuilds = _client.Guilds;
|
||||
var messageGuildAssociation = new Dictionary<ulong, ulong>();
|
||||
foreach (var messageIdUnparsed in messageIds)
|
||||
{
|
||||
var messageId = ulong.Parse(messageIdUnparsed);
|
||||
var reactions = _redis.HashGetAll($"Messages:{messageIdUnparsed.ToString()}");
|
||||
|
||||
foreach (var reaction in reactions)
|
||||
{
|
||||
_logger.Information(LogSource.Migration, $"{messageIdUnparsed.ToString()} - Starting");
|
||||
try
|
||||
{
|
||||
ulong guildId = 0;
|
||||
IRole role = null;
|
||||
|
||||
var roleId = ulong.Parse(reaction.Value);
|
||||
if (messageGuildAssociation.ContainsKey(messageId))
|
||||
{
|
||||
guildId = messageGuildAssociation[messageId];
|
||||
_logger.Information(LogSource.Migration, $"{messageIdUnparsed.ToString()} - known to be in {guildId}");
|
||||
role = _client.GetGuild(guildId).GetRole(roleId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Information(LogSource.Migration, $"{messageIdUnparsed.ToString()} - Attempting to find guild");
|
||||
IRole foundRole = null;
|
||||
try
|
||||
{
|
||||
foreach (var guild in connectedGuilds)
|
||||
{
|
||||
foundRole = guild.GetRole(roleId);
|
||||
if (foundRole != null)
|
||||
{
|
||||
role = _client.GetGuild(foundRole.Guild.Id).GetRole(ulong.Parse(reaction.Value));
|
||||
messageGuildAssociation.Add(messageId, foundRole.Guild.Id);
|
||||
}
|
||||
}
|
||||
} catch { /* ignore */ }
|
||||
|
||||
if (foundRole == null)
|
||||
{
|
||||
_logger.Warning(LogSource.Migration, $"{messageIdUnparsed.ToString()} - Could not find guild for message");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
_logger.Information(LogSource.Migration, $"{messageIdUnparsed.ToString()} - Found Role {roleId.ToString()}");
|
||||
await _reactionListener.AddRoleToListener(ulong.Parse(messageIdUnparsed), guildId, reaction.Name, role);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(LogSource.Migration, $"Failed to migrate reaction for {messageIdUnparsed.ToString()}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
await _errorHandler.HandleCommandException(e, Context);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -11,6 +11,7 @@ namespace Geekbot.net.Lib.ReactionListener
|
|||
public class ReactionListener : IReactionListener
|
||||
{
|
||||
private readonly DatabaseContext _database;
|
||||
// <messageId, <reaction, roleId>
|
||||
private Dictionary<ulong, Dictionary<IEmote, ulong>> _listener;
|
||||
|
||||
public ReactionListener(DatabaseContext database)
|
||||
|
@ -50,6 +51,7 @@ namespace Geekbot.net.Lib.ReactionListener
|
|||
RoleId = role.Id.AsLong(),
|
||||
Reaction = emoji
|
||||
});
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
if (!_listener.ContainsKey(messageId))
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue