forked from zaanposni/discord-masz
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created command to update the roles of all users in a guild
- Loading branch information
1 parent
31e18d9
commit 3159e2c
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,71 @@ | ||
using Bot.Abstractions; | ||
using Bot.Attributes; | ||
using Discord.Interactions; | ||
using Bot.Enums; | ||
using Levels.Data; | ||
using Discord; | ||
using Levels.Models; | ||
|
||
namespace Levels.Commands; | ||
|
||
public class UpdateAllXp : Command<UpdateAllXp> | ||
{ | ||
public GuildLevelConfigRepository GuildLevelConfigRepository { get; set; } | ||
public GuildUserLevelRepository GuildUserLevelRepository { get; set; } | ||
|
||
[SlashCommand("update-all-xp", "Updates all user's experience")] | ||
[Require(RequireCheck.GuildAdmin)] | ||
public async Task UpdateAllXpCommand() | ||
{ | ||
var guild = Context.Guild; | ||
var userXps = GuildUserLevelRepository.GetAllLevelsInGuild(guild.Id); | ||
var guildConfig = await GuildLevelConfigRepository!.GetOrCreateConfig(guild.Id); | ||
|
||
var roles = guildConfig.Levels.ToDictionary( | ||
x => x.Key, | ||
x => x.Value.Where(x => guild.GetRole(x) != null).ToArray() | ||
); | ||
|
||
var users = await guild.GetUsersAsync().FlattenAsync(); | ||
var count = 0; | ||
var totalCount = users.Count(); | ||
|
||
await RespondInteraction($"Updating all user roles in {guild.Name}!"); | ||
|
||
foreach (var user in users) | ||
{ | ||
var xp = userXps.FirstOrDefault(x => x.UserId == user.Id); | ||
var level = 0; | ||
|
||
if (xp != null) | ||
{ | ||
var calcLevel = new CalculatedGuildUserLevel(xp, guildConfig); | ||
level = calcLevel.Total.Level; | ||
} | ||
|
||
var addedRoles = roles.Where(x => x.Key <= level) | ||
.SelectMany(x => x.Value) | ||
.Where(x => !user.RoleIds.Contains(x)); | ||
|
||
var removedRoles = roles.Where(x => x.Key > level) | ||
.SelectMany(x => x.Value) | ||
.Where(x => user.RoleIds.Contains(x)); | ||
|
||
if (addedRoles.Any()) | ||
await user.AddRolesAsync(addedRoles); | ||
|
||
if (removedRoles.Any()) | ||
await user.RemoveRolesAsync(removedRoles); | ||
|
||
count++; | ||
|
||
if (count % 50 == 0) | ||
{ | ||
var progress = Math.Floor((double)count / totalCount * 100); | ||
await RespondInteraction($"Progress {count}/{totalCount} ({progress}%)"); | ||
} | ||
} | ||
|
||
await RespondInteraction($"Successfully updated all users in {guild.Name}!"); | ||
} | ||
} |