-
Notifications
You must be signed in to change notification settings - Fork 3
New feature for auto assign ranks by the users themselves #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Smieszkokoleszko
wants to merge
3
commits into
master
Choose a base branch
from
role-manager
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,333 @@ | ||
using ArmaforcesMissionBot.DataClasses; | ||
using Discord; | ||
using Discord.WebSocket; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
using System.Timers; | ||
using ArmaforcesMissionBot.Helpers; | ||
|
||
|
||
namespace ArmaforcesMissionBot.Handlers | ||
{ | ||
public static class StringExtensionMethods | ||
{ | ||
public static string ReplaceFirst(this string text, string search, string replace) | ||
{ | ||
int pos = text.IndexOf(search); | ||
if (pos < 0) | ||
{ | ||
return text; | ||
} | ||
return text.Substring(0, pos) + replace + text.Substring(pos + search.Length); | ||
} | ||
} | ||
public class ReactionHandler : IInstallable | ||
{ | ||
private DiscordSocketClient _client; | ||
private MiscHelper _miscHelper; | ||
private IServiceProvider _services; | ||
private Config _config; | ||
private Timer _timer; | ||
|
||
public async Task Install(IServiceProvider map) | ||
{ | ||
_client = map.GetService<DiscordSocketClient>(); | ||
_config = map.GetService<Config>(); | ||
_miscHelper = map.GetService<MiscHelper>(); | ||
_services = map; | ||
// Hook the MessageReceived event into our command handler | ||
_client.ReactionAdded += HandleReactionAdded; | ||
_client.ReactionRemoved += HandleReactionRemoved; | ||
|
||
_timer = new Timer(); | ||
_timer.Interval = 2000; | ||
|
||
_timer.Elapsed += CheckReactionTimes; | ||
_timer.AutoReset = true; | ||
_timer.Enabled = true; | ||
} | ||
|
||
private async Task HandleReactionAdded(Cacheable<IUserMessage, ulong> message, ISocketMessageChannel channel, SocketReaction reaction) | ||
{ | ||
var signups = _services.GetService<SignupsData>(); | ||
|
||
if (reaction.User.IsSpecified && !reaction.User.Value.IsBot && _config.RoleAssignChannel == channel.Id) | ||
{ | ||
await AddedReactionInRolesChannel(message, channel, reaction); | ||
} | ||
else if (reaction.User.IsSpecified && !reaction.User.Value.IsBot && signups.Missions.Any(x => x.SignupChannel == channel.Id)) | ||
{ | ||
await AddedReactionInSignupsChannel(message, channel, reaction, signups); | ||
} | ||
else if (signups.Missions.Any(x => x.SignupChannel == channel.Id) && reaction.UserId != _client.CurrentUser.Id) | ||
{ | ||
var user = _client.GetUser(reaction.UserId); | ||
Console.WriteLine($"Naprawiam reakcje po spamie {user.Username}"); | ||
var teamMsg = await channel.GetMessageAsync(message.Id) as IUserMessage; | ||
await teamMsg.RemoveReactionAsync(reaction.Emote, user); | ||
} | ||
} | ||
|
||
private async Task HandleReactionRemoved(Cacheable<IUserMessage, ulong> message, ISocketMessageChannel channel, SocketReaction reaction) | ||
{ | ||
var signups = _services.GetService<SignupsData>(); | ||
|
||
if (reaction.User.IsSpecified && !reaction.User.Value.IsBot && _config.RoleAssignChannel == channel.Id) | ||
{ | ||
await RemovedReactionInRolesChannel(message, channel, reaction); | ||
} | ||
else if (signups.Missions.Any(x => x.SignupChannel == channel.Id)) | ||
{ | ||
await RemovedReactionInSignupsChannel(message, channel, reaction, signups); | ||
} | ||
} | ||
|
||
private async Task HandleReactionChange(Cacheable<IUserMessage, ulong> message, ISocketMessageChannel channel, SocketReaction reaction, SignupsData signups) | ||
{ | ||
await signups.BanAccess.WaitAsync(-1); | ||
try | ||
{ | ||
if (!signups.ReactionTimes.ContainsKey(reaction.User.Value.Id)) | ||
{ | ||
signups.ReactionTimes[reaction.User.Value.Id] = new Queue<DateTime>(); | ||
} | ||
|
||
signups.ReactionTimes[reaction.User.Value.Id].Enqueue(DateTime.Now); | ||
|
||
Console.WriteLine($"[{DateTime.Now.ToString()}] {reaction.User.Value.Username} spam counter: {signups.ReactionTimes[reaction.User.Value.Id].Count}"); | ||
|
||
if (signups.ReactionTimes[reaction.User.Value.Id].Count >= 10 && !signups.SpamBans.ContainsKey(reaction.User.Value.Id)) | ||
{ | ||
await Helpers.BanHelper.BanUserSpam(_services, reaction.User.Value); | ||
} | ||
} | ||
finally | ||
{ | ||
signups.BanAccess.Release(); | ||
} | ||
} | ||
|
||
private async void CheckReactionTimes(object source, ElapsedEventArgs e) | ||
{ | ||
var signups = _services.GetService<SignupsData>(); | ||
|
||
await signups.BanAccess.WaitAsync(-1); | ||
try | ||
{ | ||
foreach (var user in signups.ReactionTimes) | ||
{ | ||
while (user.Value.Count > 0 && user.Value.Peek() < e.SignalTime.AddSeconds(-30)) | ||
user.Value.Dequeue(); | ||
} | ||
} | ||
finally | ||
{ | ||
signups.BanAccess.Release(); | ||
} | ||
} | ||
|
||
private async Task AddedReactionInSignupsChannel(Cacheable<IUserMessage, ulong> message, ISocketMessageChannel channel, SocketReaction reaction, SignupsData signups) | ||
{ | ||
var reactionStringAnimatedVersion = reaction.Emote.ToString().Insert(1, "a"); | ||
|
||
var mission = signups.Missions.Single(x => x.SignupChannel == channel.Id); | ||
|
||
await HandleReactionChange(message, channel, reaction, signups); | ||
Console.WriteLine($"[{DateTime.Now.ToString()}] {reaction.User.Value.Username} added reaction {reaction.Emote.Name}"); | ||
|
||
if (signups.SignupBans.ContainsKey(reaction.User.Value.Id) && signups.SignupBans[reaction.User.Value.Id] > mission.Date) | ||
{ | ||
await reaction.User.Value.SendMessageAsync("Masz bana na zapisy, nie mo�esz zapisa� si� na misj�, kt�ra odb�dzie si� w czasie trwania bana."); | ||
var teamMsg = await channel.GetMessageAsync(message.Id) as IUserMessage; | ||
await teamMsg.RemoveReactionAsync(reaction.Emote, reaction.User.Value); | ||
return; | ||
} | ||
|
||
await mission.Access.WaitAsync(-1); | ||
try | ||
{ | ||
if (mission.Teams.Any(x => x.TeamMsg == message.Id)) | ||
{ | ||
var team = mission.Teams.Single(x => x.TeamMsg == message.Id); | ||
if (team.Slots.Any(x => (x.Emoji == reaction.Emote.ToString() || x.Emoji == reactionStringAnimatedVersion) && (x.Count > x.Signed.Count() || team.Reserve != 0))) | ||
{ | ||
var teamMsg = await channel.GetMessageAsync(message.Id) as IUserMessage; | ||
|
||
var embed = teamMsg.Embeds.Single(); | ||
|
||
if (!mission.SignedUsers.Any(x => x == reaction.User.Value.Id)) | ||
{ | ||
var slot = team.Slots.Single(x => x.Emoji == reaction.Emote.ToString() || x.Emoji == reactionStringAnimatedVersion); | ||
if (!slot.Signed.Contains(reaction.User.Value.Id)) | ||
slot.Signed.Add(reaction.User.Value.Id); | ||
if (!mission.SignedUsers.Contains(reaction.User.Value.Id)) | ||
mission.SignedUsers.Add(reaction.User.Value.Id); | ||
|
||
var newDescription = _miscHelper.BuildTeamSlots(team); | ||
|
||
var newEmbed = new EmbedBuilder | ||
{ | ||
Title = team.Name, | ||
Color = embed.Color | ||
}; | ||
|
||
if (newDescription.Count == 2) | ||
newEmbed.WithDescription(newDescription[0] + newDescription[1]); | ||
else if (newDescription.Count == 1) | ||
newEmbed.WithDescription(newDescription[0]); | ||
|
||
if (embed.Footer.HasValue) | ||
newEmbed.WithFooter(embed.Footer.Value.Text); | ||
else | ||
newEmbed.WithFooter(team.Pattern); | ||
|
||
await teamMsg.ModifyAsync(x => x.Embed = newEmbed.Build()); | ||
} | ||
else | ||
{ | ||
await teamMsg.RemoveReactionAsync(reaction.Emote, reaction.User.Value); | ||
} | ||
} | ||
else if (team.Slots.Any(x => x.Emoji == reaction.Emote.ToString())) | ||
{ | ||
var teamMsg = await channel.GetMessageAsync(message.Id) as IUserMessage; | ||
await teamMsg.RemoveReactionAsync(reaction.Emote, reaction.User.Value); | ||
} | ||
} | ||
} | ||
finally | ||
{ | ||
mission.Access.Release(); | ||
} | ||
} | ||
|
||
private async Task AddedReactionInRolesChannel(Cacheable<IUserMessage, ulong> message, ISocketMessageChannel channel, SocketReaction reaction) | ||
{ | ||
var fullMessage = await channel.GetMessageAsync(message.Id) as IUserMessage; | ||
|
||
var messageDescription = fullMessage.Embeds.Single().Description; | ||
ulong rank; | ||
var reactionsMatches = MiscHelper.GetRankArrayMatchesFromText(messageDescription); | ||
var rankForEmoji = reactionsMatches.Find(x => x.Item1 == reaction.Emote.ToString()); | ||
|
||
try | ||
{ | ||
string regexPattern = @"\<\@\&([0-9]+)\>"; | ||
var rankMatches = Regex.Matches(rankForEmoji.Item2, regexPattern, RegexOptions.IgnoreCase | RegexOptions.RightToLeft).First().ToString(); | ||
rank = ulong.Parse(Regex.Matches(rankMatches, "[0-9]+", RegexOptions.IgnoreCase | RegexOptions.RightToLeft).First().Value); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine($"Parsing ranks failed."); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
var role = (channel as SocketGuildChannel).Guild.GetRole(rank); | ||
|
||
await (reaction.User.Value as SocketGuildUser).AddRoleAsync(role); | ||
|
||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Adding rank failed."); | ||
} | ||
|
||
return; | ||
} | ||
|
||
private async Task RemovedReactionInSignupsChannel(Cacheable<IUserMessage, ulong> message, ISocketMessageChannel channel, SocketReaction reaction, SignupsData signups) | ||
{ | ||
var reactionStringAnimatedVersion = reaction.Emote.ToString().Insert(1, "a"); | ||
|
||
var mission = signups.Missions.Single(x => x.SignupChannel == channel.Id); | ||
var user = await (channel as IGuildChannel).Guild.GetUserAsync(reaction.UserId); | ||
|
||
Console.WriteLine($"[{DateTime.Now.ToString()}] {user.Username} removed reaction {reaction.Emote.Name}"); | ||
|
||
await mission.Access.WaitAsync(-1); | ||
try | ||
{ | ||
if (mission.Teams.Any(x => x.TeamMsg == message.Id)) | ||
{ | ||
var team = mission.Teams.Single(x => x.TeamMsg == message.Id); | ||
if (team.Slots.Any(x => (x.Emoji == reaction.Emote.ToString() || x.Emoji == reactionStringAnimatedVersion) && x.Signed.Contains(user.Id))) | ||
{ | ||
var teamMsg = await channel.GetMessageAsync(message.Id) as IUserMessage; | ||
var embed = teamMsg.Embeds.Single(); | ||
|
||
var slot = team.Slots.Single(x => x.Emoji == reaction.Emote.ToString() || x.Emoji == reactionStringAnimatedVersion); | ||
slot.Signed.Remove(user.Id); | ||
mission.SignedUsers.Remove(user.Id); | ||
|
||
var newDescription = _miscHelper.BuildTeamSlots(team); | ||
|
||
var newEmbed = new EmbedBuilder | ||
{ | ||
Title = team.Name, | ||
Color = embed.Color | ||
}; | ||
|
||
if (newDescription.Count == 2) | ||
newEmbed.WithDescription(newDescription[0] + newDescription[1]); | ||
else if (newDescription.Count == 1) | ||
newEmbed.WithDescription(newDescription[0]); | ||
|
||
if (embed.Footer.HasValue) | ||
newEmbed.WithFooter(embed.Footer.Value.Text); | ||
else | ||
newEmbed.WithFooter(team.Pattern); | ||
|
||
await teamMsg.ModifyAsync(x => x.Embed = newEmbed.Build()); | ||
} | ||
} | ||
} | ||
finally | ||
{ | ||
mission.Access.Release(); | ||
} | ||
} | ||
|
||
private async Task RemovedReactionInRolesChannel(Cacheable<IUserMessage, ulong> message, ISocketMessageChannel channel, SocketReaction reaction) | ||
{ | ||
var fullMessage = await channel.GetMessageAsync(message.Id) as IUserMessage; | ||
|
||
var messageDescription = fullMessage.Embeds.Single().Description; | ||
ulong rank; | ||
var reactionsMatches = MiscHelper.GetRankArrayMatchesFromText(messageDescription); | ||
var rankForEmoji = reactionsMatches.Find(x => x.Item1 == reaction.Emote.ToString()); | ||
|
||
try | ||
{ | ||
string regexPattern = @"\<\@\&([0-9]+)\>"; | ||
var rankMatches = Regex.Matches(rankForEmoji.Item2, regexPattern, RegexOptions.IgnoreCase | RegexOptions.RightToLeft).First().ToString(); | ||
rank = ulong.Parse(Regex.Matches(rankMatches, "[0-9]+", RegexOptions.IgnoreCase | RegexOptions.RightToLeft).First().Value); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine($"Parsing ranks failed."); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
var role = (channel as SocketGuildChannel).Guild.GetRole(rank); | ||
|
||
await (reaction.User.Value as SocketGuildUser).RemoveRoleAsync(role); | ||
|
||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Adding rank failed."); | ||
} | ||
|
||
return; | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.