private async Task MessageReceived(SocketMessage rawMessage) { // Ignore system messages and messages from bots if (!(rawMessage is SocketUserMessage message)) { return; } if (message.Source != MessageSource.User) { return; } dbWalrusContext db = new dbWalrusContext(); int argPos = 0; if (!message.HasStringPrefix(db["config", Program.Debug ? "botDebugPrefix" : "botPrefix"], ref argPos) && !message.HasMentionPrefix(_client.CurrentUser, ref argPos)) { return; } var context = new SocketCommandContext(_client, message); var result = await _commands.ExecuteAsync(context, argPos, _provider); if (result.Error.HasValue && result.Error.Value != CommandError.UnknownCommand) { await context.Channel.SendMessageAsync(result.ToString()); } }
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously public override async Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously { var user = context.User as IGuildUser; if (user == null) { return(PreconditionResult.FromError("This command cannot be called from outside a Discord server!")); } var guild = user.Guild; List <ulong> roleIds = new List <ulong>(); dbWalrusContext database = new dbWalrusContext(); foreach (string role in _roles) { ulong roleId = 0; try { roleId = Convert.ToUInt64(database["role", role]); } catch { Console.WriteLine($"The role {role} is given in a RequireRole attribute but you haven't added it the the MySQL database!"); } if (guild.Roles.Any(r => r.Id == roleId)) { roleIds.Add(roleId); } } if (roleIds.Count < 1) { return(PreconditionResult.FromError($"The guild does not have the role any of the roles required to access this command.")); } return(user.RoleIds.Any(rId => roleIds.Contains(rId)) ? PreconditionResult.FromSuccess() : PreconditionResult.FromError("You do not have the sufficient role required to access this command.")); }
private static IRole ParseRole(dbWalrusContext database, string roleName, SocketGuild guild) { IRole role = null; if (UInt64.TryParse(database["role", roleName], out ulong roleId)) { role = guild.GetRole(roleId); } return(role); }
public static async Task RfrAddRoleAsync(IEmbed embed, SocketReaction reaction) { IGuildUser user = reaction.User.Value as IGuildUser; dbWalrusContext database = new dbWalrusContext(); if (user.GuildId == Convert.ToUInt64(database["config", "svgeServerId"]) && // if this is the main server (means it can be used in other servers) !user.RoleIds.Contains <ulong>(Convert.ToUInt64(database["role", "communityMember"])) && // and they don't have community membership !user.RoleIds.Contains <ulong>(Convert.ToUInt64(database["role", "student"]))) { return; // and aren't a student, then don't give a role. } EmbedField field = embed.Fields.First(f => f.Value.StartsWith(reaction.Emote.ToString())); int atIndex = field.Value.IndexOf('@'); ulong roleId = Convert.ToUInt64(field.Value.Remove(0, atIndex + 2).TrimEnd('>').ToString()); IRole role = user.Guild.Roles.First(r => r.Id == roleId); await user.AddRoleAsync(role); }
private static List <ulong> GetAdditionalRoleIds(WalrusUserInfo userInfo, SocketGuild guild, dbWalrusContext database) { List <ulong> customRoles = new List <ulong>(); if (userInfo.AdditionalRolesJSON != null) { // not going to try as *should* always be valid JSON if added using a command JObject customRolesJason = JObject.Parse(userInfo.AdditionalRolesJSON); foreach (var role in customRolesJason) { if (UInt64.TryParse(role.Value.ToString(), out ulong id)) { if (guild.Roles.Any(r => r.Id == id)) { customRoles.Add(id); } } } /* * foreach (string s in customRoleStrings) * { * string idString = database["role", s]; * if (idString != null) * { * if (UInt64.TryParse(idString, out ulong id)) * { * if (guild.Roles.Any(r => r.Id == id)) customRoles.Add(id); * } * } * }*/ } return(customRoles); }
// called by daily function (via above static function) or by the code command private static async Task UpdateAsync(SocketGuildUser user, SocketGuild guild, dbWalrusContext database) // not callable as a Discord command { //Console.WriteLine("Updating..."); Console.WriteLine($"Updating user information for {user.Nickname ?? user.Username}:"); if (user.IsBot) { return; } string userId = user.Id.ToString(); WalrusUserInfo userInfo = database.WalrusUserInfoes.Where(x => x.UserId == userId).FirstOrDefault(); if (userInfo == null) { Console.WriteLine($"\tUser {user.Nickname ?? user.Username} does not exist in the database!"); List <IRole> roles = (user as IGuildUser).RoleIds.ToList().ConvertAll(x => guild.GetRole(x) as IRole); Console.WriteLine($"\tNumber of roles: {roles.Count}"); foreach (IRole role in roles) { if (!role.IsManaged && !(role == guild.EveryoneRole) && !role.Name.Contains("CPS")) { try { await user.RemoveRoleAsync(role); } catch { Console.WriteLine($"\tFailed to remove role {role.Name}."); } } } return; } Console.WriteLine("\tGetting custom roles"); List <ulong> customRoleIds = GetAdditionalRoleIds(userInfo, guild as SocketGuild, database); if (!userInfo.Verified) { Console.WriteLine($"\tUser {user.Nickname ?? user.Username} is not verified!"); // remove everything that isn't a custom role List <IRole> roles = (user as IGuildUser).RoleIds.Except(customRoleIds).ToList().ConvertAll(x => guild.GetRole(x) as IRole); Console.WriteLine($"\tNumber of roles: {roles.Count}"); foreach (IRole role in roles) { if (!role.IsManaged && !(role == guild.EveryoneRole) && !role.Name.Contains("CPS")) { try { await user.RemoveRoleAsync(role); } catch { Console.WriteLine($"\tFailed to remove role {role.Name}."); } } } foreach (IRole role in customRoleIds.ConvertAll(id => guild.GetRole(id))) { if (!role.IsManaged && !(role == guild.EveryoneRole)) { try { await user.AddRoleAsync(role); } catch { Console.WriteLine($"\tFailed to add customer role {role.Name}"); } } } return; } IRole studentRole = ParseRole(database, "student", guild as SocketGuild); if (studentRole != null) { if (userInfo.Email.Split('@')[1] == database["config", "studentEmailDomain"]) { await user.AddRoleAsync(studentRole); } else { Console.WriteLine("\tRemoving student role..."); await user.RemoveRoleAsync(studentRole); } } WalrusMembershipList membership = database.WalrusMembershipLists.Where(x => x.Email == userInfo.Email).FirstOrDefault(); IRole communityMembershipRole = ParseRole(database, "communityMember", guild as SocketGuild); IRole dlcMembershipRole = ParseRole(database, "dlcMember", guild as SocketGuild); IRole gotyMembershipRole = ParseRole(database, "gotyMember", guild as SocketGuild); if (membership != null) { /* if they're in the membership list then they at least have the community membership level */ if (communityMembershipRole != null) { await user.AddRoleAsync(communityMembershipRole); } if (membership.Membership.Replace("\"", "").Split(',').Contains("DLC Bundle Membership") && dlcMembershipRole != null) { await user.AddRoleAsync(dlcMembershipRole); } if (membership.Membership.Replace("\"", "").Split(',').Contains("Game of the Year Membership") && gotyMembershipRole != null) { await user.AddRoleAsync(gotyMembershipRole); } } else { if (communityMembershipRole != null) { await user.RemoveRoleAsync(communityMembershipRole); } if (dlcMembershipRole != null) { await user.RemoveRoleAsync(dlcMembershipRole); } if (gotyMembershipRole != null) { await user.RemoveRoleAsync(gotyMembershipRole); } } //get additional roles only returns IDs for roles that exist in the guild so no need to check again foreach (IRole role in customRoleIds.ConvertAll(id => guild.GetRole(id))) { try { await user.AddRoleAsync(role); } catch { Console.WriteLine($"\tFailed to add role {role.Name}."); } } }