/// <summary> /// Retrive all eve id's from the database. /// </summary> /// <returns></returns> async Task <Dictionary <ulong, int> > GetAuthedUsers() { Dictionary <ulong, int> dbUser = new Dictionary <ulong, int>(); List <DatabaseRow> rows = await DatabaseModule.RunQueryAsync(Config.RecruitmentDatabase, "select * from users;"); foreach (DatabaseRow row in rows) { dbUser.Add((ulong)row.GetData <long>(1), row.GetData <int>(0)); } return(dbUser); }
/// <summary> /// Loop through all guild users and check if they have all assigned roles /// </summary> /// <returns></returns> public async Task VerifyRoles() { foreach (IGuildUser user in await Guild.GetUsersAsync()) { // Get assigned roles for a user. List<DatabaseRow> rows = await DatabaseModule.RunQueryAsync(Config.RoleDatabase, $"SELECT role_id FROM userRoles WHERE user_id = {user.Id};"); List<ulong> roles = rows.Select(x => (ulong)x.Data[0]).ToList(); // Check all roles user currently has. foreach (ulong role in user.RoleIds) { // Skip roles not configured to be managed. if (!Config.RolesToManage.Any(x => x.RoleID == role)) continue; bool hasRole = user.RoleIds.Any(x => x == role); // Remove role from user if he does not have permission to have it. if (hasRole && !roles.Contains(role)) await RemoveActualRole(user, role); // Add any roles to user if he does not have it. if (!hasRole && roles.Contains(role)) await AddActualRole(user, role); } // Check all assigned roles. foreach (ulong role in roles) { // Skip roles not configured to be managed. if (!Config.RolesToManage.Any(x => x.RoleID == role)) continue; bool hasRole = user.RoleIds.Any(x => x == role); // Remove role from user if he does not have permission to have it. if (hasRole && !roles.Contains(role)) await RemoveActualRole(user, role); // Add any roles to user if he does not have it. if (!hasRole && roles.Contains(role)) await AddActualRole(user, role); } } }
protected override async Task ReactionRemoved(IUserMessage message, ISocketMessageChannel channel, SocketReaction reaction) { List<DatabaseRow> rows = await DatabaseModule.RunQueryAsync(Config.RoleDatabase, $"SELECT * FROM reactionMessages where message_id = {message.Id} LIMIT 1;"); // Ignore if a bot is reacting or reaction is not on a reaction message if (reaction.User.Value.IsBot || rows.Count == 0) return; string emote = reaction.Emote.Name.GetHashCode().ToString(); ulong roleID = await GetRoleID(message.Id, emote); // Get and set the context for this call because it comes from an event. IGuildUser user = await(message.Channel as IGuildChannel).GetUserAsync(reaction.User.Value.Id); SetContext(new MethodContext(user.Guild, channel, message)); await ModifyRoleAsync(user, RoleAction.Remove, new string[] { roleID.ToString() }); await VerifyRoles(); }
/// <summary> /// Syncronize the discord roles and the databse roles. /// </summary> /// <returns></returns> public async Task SyncRoles() { foreach (IGuildUser user in await Guild.GetUsersAsync()) { // Get assigned roles for a user. List<DatabaseRow> rows = await DatabaseModule.RunQueryAsync(Config.RoleDatabase, $"SELECT role_id FROM userRoles WHERE user_id = {user.Id};"); List<ulong> roles = rows.Select(x => (ulong)x.Data[0]).ToList(); foreach (ulong role in user.RoleIds) { // Skip roles not configured to be managed. if (!Config.RolesToManage.Any(x => x.RoleID == role)) continue; IRole guildRole = Guild.GetRole(role); bool hasRole = user.RoleIds.Any(x => x == role); await ModifyRoleAsync(user, hasRole ? RoleAction.Add : RoleAction.Remove, guildRole.Name); } } }
public async Task Auth( [Summary("Discord user ID, Enable developer mode and rick click user.")] ulong discordId, [Summary("Eve user ID, Go to thei ZKill page and copy the ID from the URL.")] int eveId) { IGuildUser user = await Guild.GetUserAsync(discordId); Dictionary <ulong, int> authedUsers = await GetAuthedUsers(); if (authedUsers.TryGetValue(discordId, out int authedChar)) { await RespondAsync($"User already authed to {authedChar}, if bot does not update roles run !namecheck or !auth loop restart. If that does not work pray Prople is online.", false, false); return; } string name = string.IsNullOrEmpty(user.Nickname) ? user.Username : user.Nickname; await RespondAsync($"Authenticating {name}...", false, false); await DatabaseModule.RunQueryAsync(Config.RecruitmentDatabase, $"INSERT INTO users VALUES ({eveId},{discordId})"); await RespondAsync("Done, run !namecheck to update roles.", false, false); }
/// <summary> /// Resolve reaction message emote to get role id. /// </summary> /// <param name="msgId"></param> /// <param name="emote"></param> /// <returns></returns> async Task<ulong> GetRoleID(ulong msgId, string emote) { List<DatabaseRow> rows = await DatabaseModule.RunQueryAsync(Config.RoleDatabase, $"SELECT role_id FROM reactionMessages where message_id = {msgId} AND emote = '{emote}' LIMIT 1;"); return (ulong)rows[0].Data[0]; }