public async Task SetOpenDMAsync() { SocketUser author = Context.User; ulong userId = author.Id; SocketGuildChannel guildChannel = Context.Channel as SocketGuildChannel; List <SocketRole> roles = guildChannel.Guild.Roles.ToList(); using (DiscordContext db = new DiscordContext()) { User user = db.Users.FirstOrDefault(x => x.DiscordId == userId); if (user != null) { Predicate <SocketRole> RoleFinder = (SocketRole sr) => { return(sr.Name == Roles.OpenDM); }; SocketRole AnnouncementRole = roles.Find(RoleFinder); AnnouncementRole = roles.Find(RoleFinder); // Check if role was found on the server if (AnnouncementRole == null) { Logger.Warning("System", $"Couldn't find role {Roles.OpenDM}"); await BotReporting.ReportAsync(ReportColors.exception, Context.Channel as SocketTextChannel, "Error finding Role.", $"Could not find role: {Roles.OpenDM}, contact admin.", Context.User); await ReplyAsync("Error finding role, notifying staff."); } if (user.OpenDM == true) { // Remove the role await guildChannel.GetUser((ulong)userId).RemoveRoleAsync(AnnouncementRole); Logger.Verbose("System", $"found role {AnnouncementRole.Name} and removed it."); user.OpenDM = false; await ReplyAsync($"<@{userId}> left the `{Roles.OpenDM}` role."); } else { // Add the role await guildChannel.GetUser((ulong)userId).AddRoleAsync(AnnouncementRole); Logger.Verbose("System", $"Found role {AnnouncementRole.Name} and added it."); user.OpenDM = true; await ReplyAsync($"<@{userId}> joined the `{Roles.OpenDM}` role."); } db.SaveChanges(); } else { Logger.Verbose(author.Username, $"Failed to find user: {userId}"); await ReplyAsync($"Failed to find user: `{author.Username}`"); } } }
public async Task SetMonkAsync(string parameter) { SocketUser author = Context.User; ulong userId = author.Id; SocketGuildChannel guildChannel = Context.Channel as SocketGuildChannel; List <SocketRole> roles = guildChannel.Guild.Roles.ToList(); if (parameter == "no") { Logger.Info(author.Username, $"Removing Monk role for <@{userId}>."); // Remove old role Predicate <SocketRole> MonkFinder = (SocketRole sr) => { return(sr.Name == "Monk"); }; SocketRole MonkRole = roles.Find(MonkFinder); if (MonkRole != null) { await guildChannel.GetUser(userId).RemoveRoleAsync(MonkRole); Logger.Verbose("System", $"found role {MonkRole.Name} and removed it."); await ReplyAsync($"<@{userId}> is now allowed into the RP rooms."); } else { Logger.Warning("System", $"Couldn't find role Monk."); } } else if (parameter == "yes") { // Adding role to user Predicate <SocketRole> Monkfinder = (SocketRole sr) => { return(sr.Name == "Monk"); }; SocketRole MonkRole = roles.Find(Monkfinder); await guildChannel.GetUser(userId).AddRoleAsync(MonkRole); if (MonkRole != null) { await ReplyAsync($"<@{userId}> has been removed from RP rooms."); } else { Logger.Warning("System", $"Couldn't find role Monk."); } } else { await ReplyAsync($"Sorry I couldn't understand. Please answer `yes` or `no`"); } }
private async Task <bool> Respond(ulong toID, ulong userID, string message) { string[] query = StripCommand(message, Options.ChatCommand.Command); if (query != null && query.Length == 1) { await SendMessageAfterDelay(toID, "Usage: " + Options.ChatCommand.Command + " <@user> - kicks a user from chat"); return(true); } else if (query != null && query.Length == 2) { SocketGuildChannel channel = Bot.Client.GetChannel(toID) as SocketGuildChannel; int from = query[1].IndexOf("<@") + "<@".Length; int to = query[1].LastIndexOf(">"); string result = query[1].Substring(from, to - from); SocketGuildUser user = channel.GetUser(Convert.ToUInt64(result)); if (user == null) { await SendMessageAfterDelay(toID, "User not found!"); return(true); } Log.Instance.Info("Kicking {0} from {1}", user.Username, channel.Name); await user.KickAsync(); await SendMessageAfterDelay(toID, string.Format("Kicked {0} from {1}!", user.Username, channel.Name)); return(true); } return(false); }
public override async Task ProcessAsync(SocketMessage message, string[] parameters) { ulong userId = message.Author.Id; foreach (ulong mod in UserIds.Mods) { if (userId == mod) { // Check if command params are correct. if (parameters.Length != 1) { Logger.Verbose(message.Author.Username, "Failed unmute command"); await message.Channel.SendMessageAsync("Error: Wrong syntax, try kunmute [user]."); return; } // Check if user attached is correct. if (message.MentionedUsers.Count() == 0) { Logger.Verbose(message.Author.Username, "Failed unmute command"); await message.Channel.SendMessageAsync($"Error: No user mentioned, try kunmute [user]."); return; } ulong user = message.MentionedUsers.FirstOrDefault().Id; SocketGuildChannel guildChannel = message.Channel as SocketGuildChannel; List <SocketRole> roles = guildChannel.Guild.Roles.ToList(); try { Predicate <SocketRole> roleFinder = (SocketRole sr) => { return(sr.Name.ToLower() == "mute"); }; SocketRole role = roles.Find(roleFinder); await guildChannel.GetUser((ulong)user).RemoveRoleAsync(role); await message.Channel.SendMessageAsync($"<@{user}>, You have been unmuted."); Logger.Warning(message.Author.Username, $"Has been unmuted"); } catch (Exception e) { await message.Channel.SendMessageAsync($"<@{user}>, Sorry, either you mis-spelt the role or i dont have permission to remove that role."); Logger.Warning(message.Author.Username, $"Command failed: {e.Message}"); } return; } } Logger.Warning(message.Author.Username, "Tried to use unmute command"); await message.Channel.SendMessageAsync("Sorry you do not have permission to use this command"); return; }
private bool IsViewableTextChannel(SocketGuildChannel c) { if (!(c is SocketTextChannel)) { return(false); } return(c.GetUser(_discord.Client.CurrentUser.Id) != null); }
public static bool HaveAccess(this SocketGuildChannel channel, SocketGuildUser user) { if (channel.GetUser(user.Id) != null) { return(true); } if (channel.PermissionOverwrites.Count == 0) { return(true); // Default permissions. Access all } var overwrite = channel.GetPermissionOverwrite(user); if (overwrite != null) { // Specific user overwrite if (overwrite.Value.ViewChannel == PermValue.Allow) { return(true); } else if (overwrite.Value.ViewChannel == PermValue.Deny) { return(false); } } var everyonePerm = channel.GetPermissionOverwrite(user.Guild.EveryoneRole); var isEveryonePerm = everyonePerm != null && (everyonePerm.Value.ViewChannel == PermValue.Allow || everyonePerm.Value.ViewChannel == PermValue.Inherit); foreach (var role in user.Roles.Where(o => !o.IsEveryone)) { var roleOverwrite = channel.GetPermissionOverwrite(role); if (roleOverwrite == null) { continue; } if (roleOverwrite.Value.ViewChannel == PermValue.Deny && isEveryonePerm) { return(false); } if (roleOverwrite.Value.ViewChannel == PermValue.Allow) { return(true); } } return(isEveryonePerm); }
public override async Task ProcessAsync(SocketMessage message, string[] parameters) { if (parameters.Length == 0) { Logger.Verbose(message.Author.Username, "Failed remove command"); await message.Channel.SendMessageAsync("Error: Wrong syntax, try kremove `role`."); return; } ulong user = message.Author.Id; string roleName = string.Join(" ", parameters).ToLower(); SocketGuildChannel guildChannel = message.Channel as SocketGuildChannel; List <SocketRole> roles = guildChannel.Guild.Roles.ToList(); try { Predicate <SocketRole> roleFinder = (SocketRole sr) => { return(sr.Name.ToLower() == roleName); }; SocketRole role = roles.Find(roleFinder); foreach (string ur in Unassignable.Roles) { if (role.Name.ToLower() == ur.ToLower()) { throw new Exception("Forbidden"); } } await guildChannel.GetUser((ulong)user).RemoveRoleAsync(role); await message.Channel.SendMessageAsync($"<@{user}>, I have removed the role: {role.Name}"); Logger.Verbose(message.Author.Username, $"Removed: {role.Name}"); } catch (Exception e) { await message.Channel.SendMessageAsync($"<@{user}>, Sorry, either you mis-spelt the role or i dont have permission to remove that role."); Logger.Verbose(message.Author.Username, $"Command failed: {e.Message}"); } }
public override async Task ProcessAsync(SocketMessage message, string[] parameters) { ulong user = message.Author.Id; string roleName = parameters[0]; SocketGuildChannel guildChannel = message.Channel as SocketGuildChannel; List <SocketRole> roles = guildChannel.Guild.Roles.ToList(); Predicate <SocketRole> roleFinder = (SocketRole sr) => { return(sr.Name == roleName); }; SocketRole role = roles.Find(roleFinder); if (role != null) { await guildChannel.GetUser((ulong)user).AddRoleAsync(role); await message.Channel.SendMessageAsync($"role given"); } else { await message.Channel.SendMessageAsync($"ERROR"); } }
private async Task Client_MessageDeleted(Cacheable <IMessage, ulong> arg1, ISocketMessageChannel arg2) { var arg = await arg1.GetOrDownloadAsync(); LoggedMessage delmess; GuildSetup setup; using (var db = new DatabaseContext()) { setup = db.GuildSetups.FirstOrDefault(x => x.GuildId == (arg2 as SocketGuildChannel).Guild.Id); var message = db.LoggedMessages.FirstOrDefault(x => x.MessageId == arg1.Id); delmess = message; if (message != null) { message.IsDeleted = true; db.SaveChanges(); } } if (setup?.RemoveChannelId == 0) { return; } bool noAttachments = true; if (arg?.Attachments?.Count != null) { if (arg.Attachments.Count != 0) { noAttachments = false; } } if (arg == null && noAttachments && string.IsNullOrEmpty(delmess.Content)) { return; } if (arg.Channel.Id == 341230361251938315) { return; } if (DateTime.Now.AddMilliseconds(-500) > lastMessage && delmess.ChannelId != 324321707777196034) { List <EmbedFieldBuilder> fields = new List <EmbedFieldBuilder>(); if ((arg?.Content?.Length ?? delmess.Content.Length) > 0) { fields.Add(new EmbedFieldBuilder { Name = "Content:", Value = $"```{arg?.Content ?? delmess.Content}```" }); } if (!noAttachments) { if (arg.Attachments.Count == 1) { fields.Add(new EmbedFieldBuilder { Name = "Attachments:", Value = $"```{arg.Attachments.First().Filename}```" }); } } if (fields.Count == 0) { return; } SocketUser user = null; SocketGuildChannel chan = null; if (arg == null) { chan = (client.GetGuild(delmess.GuildId)).GetChannel(delmess.ChannelId); } if (arg == null) { user = chan.GetUser(delmess.AuthorId); } if (arg.Content == "?cleanup") { return; } await(arg2 as SocketGuildChannel).Guild.GetTextChannel(setup.RemoveChannelId).SendMessageAsync("", false, new EmbedBuilder { Title = $"A message has been deleted in {arg?.Channel?.Name ?? chan.Name}!", Fields = fields, Footer = new EmbedFooterBuilder { Text = $"Author: {arg?.Author?.Username ?? user.Username}", IconUrl = arg?.Author?.GetAvatarUrl() ?? user.GetAvatarUrl() }, Color = new Color(178, 224, 40), ThumbnailUrl = arg?.Author?.GetAvatarUrl() ?? user.GetAvatarUrl(), Timestamp = arg?.Timestamp ?? delmess.Timestamp, }.WithUrl("http://heeeeeeeey.com/")); } lastMessage = DateTime.Now; }
public async Task SetSfwAsync(string parameter) { SocketUser author = Context.User; ulong userId = author.Id; SocketGuildChannel guildChannel = Context.Channel as SocketGuildChannel; List <SocketRole> roles = guildChannel.Guild.Roles.ToList(); if (parameter == "no") { //Check for underage using (DiscordContext db = new DiscordContext()) { User user = db.Users.FirstOrDefault(x => x.DiscordId == userId); if (user != null) { if (user.Age < 18) { // Add bot reporting about attempt await ReplyAsync($"You must be 18 or older to access NSFW rooms."); return; } } else { await ReplyAsync($"An error occurred, you are not registered as a user."); return; } } Logger.Info(author.Username, $"Removing SFW role for <@{userId}>."); // Remove old role Predicate <SocketRole> SfwFinder = (SocketRole sr) => { return(sr.Name == "SFW"); }; SocketRole SfwRole = roles.Find(SfwFinder); if (SfwRole != null) { await guildChannel.GetUser(userId).RemoveRoleAsync(SfwRole); Logger.Verbose("System", $"found role {SfwRole.Name} and removed it."); await ReplyAsync($"<@{userId}> is now allowed into the NSFW rooms."); } else { Logger.Warning("System", $"Couldn't find role SWF."); } } else if (parameter == "yes") { // Adding role to user Predicate <SocketRole> Sfwfinder = (SocketRole sr) => { return(sr.Name == "SFW"); }; SocketRole SfwRole = roles.Find(Sfwfinder); await guildChannel.GetUser(userId).AddRoleAsync(SfwRole); if (SfwRole != null) { await ReplyAsync($"<@{userId}> has been removed from the NSFW rooms."); } else { Logger.Warning("System", $"Couldn't find role SWF."); } } else { await ReplyAsync($"Sorry I couldn't understand. Please answer `yes` or `no`"); return; } }
public async Task SetOrientationAsync(string parameter) { User.Orientation orientation = EnumParsers.StringToOrientation(parameter); if (orientation == User.Orientation.None) { await ReplyAsync("Couldn't understand that orientation... it can either be\n" + "```\n" + "- Straight\n" + "- Gay\n" + "- Bisexual\n" + "- Asexual\n" + "- Pansexual\n" + "- Demisexual\n" + "- Other" + "```"); return; } using (DiscordContext db = new DiscordContext()) { SocketUser author = Context.User; ulong userId = author.Id; User user = db.Users.FirstOrDefault(x => x.DiscordId == userId); if (user != null) { Logger.Info(author.Username, $"Changing @<{userId}> orientation to {orientation.ToString()}."); SocketGuildChannel guildChannel = Context.Channel as SocketGuildChannel; List <SocketRole> roles = guildChannel.Guild.Roles.ToList(); // Remove old role Predicate <SocketRole> orientationFinder = (SocketRole sr) => { return(sr.Name == user.orientation.ToString().ToLower()); }; SocketRole orientationRole = roles.Find(orientationFinder); if (orientationRole != null) { await guildChannel.GetUser(userId).RemoveRoleAsync(orientationRole); Logger.Verbose("System", $"found role {orientationRole.Name} and removed it."); } else { Logger.Warning("System", $"Couldn't find role {user.orientation.ToString().ToLower()}."); } // Adding role to user orientationFinder = (SocketRole sr) => { return(sr.Name == orientation.ToString().ToLower()); }; orientationRole = roles.Find(orientationFinder); await guildChannel.GetUser(userId).AddRoleAsync(orientationRole); user.orientation = orientation; db.SaveChanges(); await ReplyAsync($"<@{userId}> orientation is now {user.orientation.ToString().ToLower()}"); return; } Logger.Verbose(author.Username, $"Failed to find user: {userId}"); await ReplyAsync($"Failed to find user: `{author.Username}`"); } }
public async Task SetGenderAsync(string parameter) { User.Genders gender = EnumParsers.StringToGender(parameter); if (gender == User.Genders.Null) { await ReplyAsync("Couldn't understand that gender... it can either be\n" + "```\n" + "- Male\n" + "- Female\n" + "- Trans-Female\n" + "- Trans-Male\n" + "- Other\n" + "- None \n" + "- Fluid \n" + "```"); return; } using (DiscordContext db = new DiscordContext()) { SocketUser author = Context.User; ulong userId = author.Id; User user = db.Users.FirstOrDefault(x => x.DiscordId == userId); if (user != null) { Logger.Warning("AdminSetCmd", $"Setting @<{userId}>'s gender to {gender.ToString()}."); SocketGuildChannel guildChannel = Context.Channel as SocketGuildChannel; List <SocketRole> roles = guildChannel.Guild.Roles.ToList(); // Remove old role Predicate <SocketRole> genderFinder = (SocketRole sr) => { return(sr.Name == user.Gender.ToString().ToLower()); }; SocketRole genderRole = roles.Find(genderFinder); genderRole = roles.Find(genderFinder); if (genderRole != null) { await guildChannel.GetUser((ulong)userId).RemoveRoleAsync(genderRole); Logger.Verbose("System", $"found role {genderRole.Name} and removed it."); } else { Logger.Warning("System", $"Couldn't find role {user.Gender.ToString().ToLower()}."); } // Add new role genderFinder = (SocketRole sr) => { return(sr.Name == gender.ToString().ToLower()); }; genderRole = roles.Find(genderFinder); await guildChannel.GetUser((ulong)userId).AddRoleAsync(genderRole); user.Gender = gender; db.SaveChanges(); await ReplyAsync($"Changed <@{userId}>'s gender to {gender.ToString().ToLower()}"); return; } Logger.Verbose(author.Username, $"Failed to find user: {userId}"); await ReplyAsync($"Failed to find user: `{author.Username}`"); } }
public async Task ReactionChange(SocketReaction reaction, bool isAdd) { SocketGuildChannel guildChannel = reaction.Channel as SocketGuildChannel; //Bots shouldn't react to itself, should only react to roles emotes, and only to users. if (guildChannel == null || !roles.ContainsKey(reaction.MessageId) || reaction.UserId == _client.CurrentUser.Id) { return; } if (!rolesChannels.ContainsKey(guildChannel.Guild.Id)) { return; } if (guildChannel.Id != rolesChannels[guildChannel.Guild.Id]) { return; } IEmote emote = reaction.Emote; SocketGuildUser user = guildChannel.GetUser(reaction.UserId); if (user != null && roles[reaction.MessageId].ContainsKey(emote.Name)) { string roleString = roles[reaction.MessageId][emote.Name]; foreach (var role in guildChannel.Guild.Roles) { bool hasRole = false; if (role.Mention == roleString) { foreach (SocketRole testRole in user.Roles) { if (testRole.Id == role.Id) { hasRole = true; } } if (isAdd) { if (!hasRole) { await user.AddRoleAsync(role); Console.WriteLine($"{user.Username} (ID: {user.Id}) added to role {role.Name} ID: ({role.Id})"); } else { Console.WriteLine($"{user.Username} (ID: {user.Id}) already has role {role.Name} ID: ({role.Id})"); } } if (!isAdd) { if (hasRole) { await user.RemoveRoleAsync(role); Console.WriteLine($"{user.Username} (ID: {user.Id}) removed from role {role.Name} ID: ({role.Id})"); } else { Console.WriteLine($"{user.Username} (ID: {user.Id}) does not have role {role.Name} ID: ({role.Id})"); } } } } } }