public List <Command> Init(IValkyrjaClient iClient) { this.Client = iClient as ValkyrjaClient; List <Command> commands = new List <Command>(); // !memo Command newCommand = new Command("memo"); newCommand.Type = CommandType.Standard; newCommand.Description = "Display your (or other) memo."; newCommand.ManPage = new ManPage("[@user]", "`[@user]` - Optional username or mention to display memo of someone else."); newCommand.RequiredPermissions = PermissionType.Everyone; newCommand.OnExecute += async e => { if (!e.Server.Config.MemoEnabled) { await e.SendReplySafe("Memo is disabled on this server."); return; } ServerContext dbContext = ServerContext.Create(this.Client.DbConnectionString); string response = ""; if (!string.IsNullOrEmpty(e.TrimmedMessage)) { string expression = e.TrimmedMessage.ToLower(); SocketUser user = e.Message.MentionedUsers.FirstOrDefault(); if (user == null) { user = e.Server.Guild.Users.FirstOrDefault(u => (u?.Username != null && u.Username.ToLower() == expression) || (u?.Nickname != null && u.Nickname.ToLower() == expression)); } if (user == null) { response = NotFoundString; } else { UserData userData = dbContext.GetOrAddUser(e.Server.Id, user.Id); if (string.IsNullOrEmpty(userData.Memo)) { response = string.Format(NoMemoOtherString, user.GetNickname(), e.CommandId); } else { response = string.Format(MemoOtherString, user.GetNickname(), e.CommandId, userData.Memo); } } } else { UserData userData = dbContext.GetOrAddUser(e.Server.Id, e.Message.Author.Id); if (string.IsNullOrEmpty(userData.Memo)) { response = string.Format(NoMemoString, e.CommandId); } else { response = string.Format(MemoString, e.CommandId, userData.Memo); } } dbContext.Dispose(); await e.SendReplySafe(response); }; commands.Add(newCommand); // !setMemo newCommand = new Command("setMemo"); newCommand.Type = CommandType.Standard; newCommand.Description = "Set your memo."; newCommand.ManPage = new ManPage("<memo text>", "`<memo text>` - Text to be recorded as your memo."); newCommand.RequiredPermissions = PermissionType.Everyone; newCommand.OnExecute += async e => { if (!e.Server.Config.MemoEnabled) { await e.SendReplySafe("Memo is disabled on this server."); return; } ServerContext dbContext = ServerContext.Create(this.Client.DbConnectionString); string response = ""; UserData userData = dbContext.GetOrAddUser(e.Server.Id, e.Message.Author.Id); if (string.IsNullOrEmpty(e.TrimmedMessage)) { userData.Memo = ""; response = ClearedString; } else { userData.Memo = e.TrimmedMessage; response = string.Format(SetString, userData.Memo); } dbContext.SaveChanges(); dbContext.Dispose(); await e.SendReplySafe(response); }; commands.Add(newCommand); // !profile newCommand = new Command("profile"); newCommand.Type = CommandType.Standard; newCommand.Description = "Display your (or other) profile. Get Help: setProfile --help"; newCommand.ManPage = new ManPage("[@user]", "`[@user]` - Optional username or mention to display profile of someone else."); newCommand.RequiredPermissions = PermissionType.Everyone; newCommand.OnExecute += async e => { if (!this.Client.IsPremium(e.Server) && !this.Client.IsTrialServer(e.Server.Id)) { await e.SendReplySafe("User profiles are a subscriber-only feature."); return; } if (!e.Server.Config.ProfileEnabled) { await e.SendReplySafe("User profiles are disabled on this server."); return; } ServerContext dbContext = ServerContext.Create(this.Client.DbConnectionString); if (!string.IsNullOrEmpty(e.TrimmedMessage)) { string expression = e.TrimmedMessage.ToLower(); SocketUser user = e.Message.MentionedUsers.FirstOrDefault(); if (user == null) { user = e.Server.Guild.Users.FirstOrDefault(u => (u?.Username != null && u.Username == e.TrimmedMessage) || (u?.Nickname != null && u.Nickname == e.TrimmedMessage)); } if (user == null) { user = e.Server.Guild.Users.FirstOrDefault(u => (u?.Username != null && u.Username.ToLower() == expression) || (u?.Nickname != null && u.Nickname.ToLower() == expression)); } if (user == null) { user = e.Server.Guild.Users.FirstOrDefault(u => (u?.Username != null && u.Username.ToLower().Contains(expression)) || (u?.Nickname != null && u.Nickname.ToLower().Contains(expression))); } if (user == null) { await e.SendReplySafe(NotFoundString); } else if (user.Id == this.Client.DiscordClient.CurrentUser.Id) { await e.SendReplySafe(null, embed : GetValkyrjaEmbed(user as SocketGuildUser)); } else { await e.SendReplySafe(null, embed : GetProfileEmbed(dbContext, e.Server, user as SocketGuildUser)); } } else { await e.SendReplySafe(null, embed : GetProfileEmbed(dbContext, e.Server, e.Message.Author as SocketGuildUser)); } dbContext.Dispose(); }; commands.Add(newCommand); // !sendProfile newCommand = new Command("sendProfile"); newCommand.Type = CommandType.Standard; newCommand.Description = "Send your profile to preconfigured introduction channel. Get Help: setProfile --help"; newCommand.ManPage = new ManPage("", ""); newCommand.RequiredPermissions = PermissionType.Everyone; newCommand.OnExecute += async e => { if (!this.Client.IsPremium(e.Server) && !this.Client.IsTrialServer(e.Server.Id)) { await e.SendReplySafe("User profiles are a subscriber-only feature."); return; } IMessageChannel channel = null; if (!e.Server.Config.ProfileEnabled || e.Server.Config.ProfileChannelId == 0 || (channel = e.Server.Guild.GetTextChannel(e.Server.Config.ProfileChannelId)) == null) { await e.SendReplySafe("User profiles do not have a channel configured."); return; } List <IMessage> messages = new List <IMessage>(); await foreach (IReadOnlyCollection <IMessage> list in channel.GetMessagesAsync(0, Direction.After, 1000, CacheMode.AllowDownload)) { IMessage message = list.FirstOrDefault(m => guid.TryParse(this.UserIdRegex.Match(m.Content).Value, out guid id) && id == e.Message.Author.Id); if (message != null) { await message.DeleteAsync(); } } ServerContext dbContext = ServerContext.Create(this.Client.DbConnectionString); await channel.SendMessageAsync($"<@{e.Message.Author.Id}>'s introduction:", embed : GetProfileEmbed(dbContext, e.Server, e.Message.Author as SocketGuildUser)); await e.SendReplySafe("It haz been done."); dbContext.Dispose(); }; commands.Add(newCommand); // !getProfile newCommand = new Command("getProfile"); newCommand.Type = CommandType.Standard; newCommand.Description = "Get the source used to set your profile."; newCommand.ManPage = new ManPage("", ""); newCommand.RequiredPermissions = PermissionType.Everyone; newCommand.OnExecute += async e => { if (!this.Client.IsPremium(e.Server) && !this.Client.IsTrialServer(e.Server.Id)) { await e.SendReplySafe("User profiles are a subscriber-only feature."); return; } if (!e.Server.Config.ProfileEnabled) { await e.SendReplySafe("User profiles are disabled on this server."); return; } StringBuilder response = new StringBuilder(); ServerContext dbContext = ServerContext.Create(this.Client.DbConnectionString); IEnumerable <ProfileOption> options = dbContext.ProfileOptions.AsQueryable().Where(o => o.ServerId == e.Server.Id).AsEnumerable().OrderBy(o => o.Order); foreach (ProfileOption option in options) { UserProfileOption userOption = dbContext.UserProfileOptions.AsQueryable().FirstOrDefault(o => o.ServerId == e.Server.Id && o.UserId == e.Message.Author.Id && o.Option == option.Option); if (userOption == null || string.IsNullOrWhiteSpace(userOption.Value)) { continue; } response.Append($"{userOption.Option} {userOption.Value} "); } response.Append("\n```"); string responseString = "There ain't no profile to get! >_<"; if (response.Length > 0) { responseString = $"```\n{e.Server.Config.CommandPrefix}setProfile {response.ToString()}"; } await e.SendReplySafe(responseString); dbContext.Dispose(); }; commands.Add(newCommand); // !setProfile newCommand = new Command("setProfile"); newCommand.Type = CommandType.Standard; newCommand.Description = "Set your profile."; newCommand.ManPage = new ManPage("[--help]", "`[--help]` - Display options configured for this server."); newCommand.RequiredPermissions = PermissionType.Everyone; newCommand.OnExecute += async e => { if (!this.Client.IsPremium(e.Server) && !this.Client.IsTrialServer(e.Server.Id)) { await e.SendReplySafe("User profiles are a subscriber-only feature."); return; } if (!e.Server.Config.ProfileEnabled) { await e.SendReplySafe("User profiles are disabled on this server."); return; } ServerContext dbContext = ServerContext.Create(this.Client.DbConnectionString); StringBuilder response = new StringBuilder(); IEnumerable <ProfileOption> options = dbContext.ProfileOptions.AsQueryable().Where(o => o.ServerId == e.Server.Id).AsEnumerable().OrderBy(o => o.Order); int maxOptionLength = 0; foreach (ProfileOption option in options) { if (maxOptionLength < option.OptionAlt.Length) { maxOptionLength = option.OptionAlt.Length; } } MatchCollection emptyOptions = this.ProfileEmptyOptionRegex.Matches(e.TrimmedMessage); if (string.IsNullOrEmpty(e.TrimmedMessage) || (emptyOptions.Count > 0 && emptyOptions.Cast <Match>().Any(o => o.Value == "-h" || o.Value == "--help"))) { response.AppendLine("```md\nSet your profile fields with the following parameters:"); foreach (ProfileOption o in options) { response.Append($" [ {o.Option} ][ {o.OptionAlt}"); response.Append(' ', maxOptionLength - o.OptionAlt.Length); response.AppendLine($" ] | {o.Label}"); } response.AppendLine($"\nExample:\n {e.Server.Config.CommandPrefix}{e.CommandId} --twitter @RheaAyase -y youtube.com/RheaAyase"); response.AppendLine($"\nTo null one of the options you have already set, leave it empty:\n {e.Server.Config.CommandPrefix}{e.CommandId} --twitter -y\n```"); dbContext.Dispose(); await e.SendReplySafe(response.ToString()); return; } foreach (Match match in emptyOptions) { ProfileOption option = options.FirstOrDefault(o => o.Option == match.Value || o.OptionAlt == match.Value); if (option == null) { continue; } UserProfileOption userOption = dbContext.UserProfileOptions.AsQueryable().FirstOrDefault(o => o.ServerId == e.Server.Id && o.UserId == e.Message.Author.Id && o.Option == option.Option); if (userOption == null) { continue; } dbContext.UserProfileOptions.Remove(userOption); } MatchCollection matches = this.ProfileParamRegex.Matches(e.TrimmedMessage); foreach (Match match in matches) { string optionString = this.ProfileOptionRegex.Match(match.Value).Value; string value = match.Value.Substring(optionString.Length + 1).Replace('`', '\''); if (value.Length >= UserProfileOption.ValueCharacterLimit) { await e.SendReplySafe($"`{optionString}` is too long! (It's {value.Length} characters while the limit is {UserProfileOption.ValueCharacterLimit})"); dbContext.Dispose(); return; } ProfileOption option = options.FirstOrDefault(o => o.Option == optionString || o.OptionAlt == optionString); if (option == null) { await e.SendReplySafe($"Unknown option: `{optionString}`"); dbContext.Dispose(); return; } UserProfileOption userOption = dbContext.UserProfileOptions.AsQueryable().FirstOrDefault(o => o.ServerId == e.Server.Id && o.UserId == e.Message.Author.Id && o.Option == option.Option); if (userOption == null) { userOption = new UserProfileOption() { ServerId = e.Server.Id, UserId = e.Message.Author.Id, Option = option.Option }; dbContext.UserProfileOptions.Add(userOption); } userOption.Value = value; } dbContext.SaveChanges(); await e.Channel.SendMessageAsync("", embed : GetProfileEmbed(dbContext, e.Server, e.Message.Author as SocketGuildUser)); dbContext.Dispose(); }; commands.Add(newCommand); return(commands); }
public List <Command> Init(IBotwinderClient iClient) { this.Client = iClient as BotwinderClient; List <Command> commands = new List <Command>(); // !memo Command newCommand = new Command("memo"); newCommand.Type = CommandType.Standard; newCommand.Description = "Display your memo. If used with a username or @mention, it will display someone else' memo."; newCommand.RequiredPermissions = PermissionType.Everyone; newCommand.OnExecute += async e => { if (!e.Server.Config.MemoEnabled) { await e.SendReplySafe("Memo is disabled on this server."); return; } ServerContext dbContext = ServerContext.Create(this.Client.DbConnectionString); string response = ""; if (!string.IsNullOrEmpty(e.TrimmedMessage)) { string expression = e.TrimmedMessage.ToLower(); SocketUser user = e.Message.MentionedUsers.FirstOrDefault(); if (user == null) { user = e.Server.Guild.Users.FirstOrDefault(u => (u?.Username != null && u.Username.ToLower() == expression) || (u?.Nickname != null && u.Nickname.ToLower() == expression)); } if (user == null) { response = NotFoundString; dbContext.Dispose(); } else { UserData userData = dbContext.GetOrAddUser(e.Server.Id, user.Id); if (string.IsNullOrEmpty(userData.Memo)) { response = string.Format(NoMemoOtherString, user.GetNickname(), e.CommandId); } else { response = string.Format(MemoOtherString, user.GetNickname(), e.CommandId, userData.Memo); } } } else { UserData userData = dbContext.GetOrAddUser(e.Server.Id, e.Message.Author.Id); if (string.IsNullOrEmpty(userData.Memo)) { response = string.Format(NoMemoString, e.CommandId); } else { response = string.Format(MemoString, e.CommandId, userData.Memo); } } await e.SendReplySafe(response); dbContext.Dispose(); }; commands.Add(newCommand); // !setMemo newCommand = new Command("setMemo"); newCommand.Type = CommandType.Standard; newCommand.Description = "Set your memo."; newCommand.RequiredPermissions = PermissionType.Everyone; newCommand.OnExecute += async e => { if (!e.Server.Config.MemoEnabled) { await e.SendReplySafe("Memo is disabled on this server."); return; } ServerContext dbContext = ServerContext.Create(this.Client.DbConnectionString); string response = ""; UserData userData = dbContext.GetOrAddUser(e.Server.Id, e.Message.Author.Id); if (string.IsNullOrEmpty(e.TrimmedMessage)) { userData.Memo = ""; response = ClearedString; } else { userData.Memo = e.TrimmedMessage; response = string.Format(SetString, userData.Memo); } dbContext.SaveChanges(); dbContext.Dispose(); await e.SendReplySafe(response); }; commands.Add(newCommand); commands.Add(newCommand.CreateAlias("setmemo")); // !profile newCommand = new Command("profile"); newCommand.Type = CommandType.Standard; newCommand.Description = "Display your profile. If used with a username or @mention, it will display someone else' profile. Get Help: setProfile --help"; newCommand.RequiredPermissions = PermissionType.Everyone; newCommand.OnExecute += async e => { if (!this.Client.IsPremium(e.Server) && !this.Client.IsTrialServer(e.Server.Id)) { await e.SendReplySafe("User profiles are a subscriber-only feature."); return; } if (!e.Server.Config.ProfileEnabled) { await e.SendReplySafe("User profiles are disabled on this server."); return; } ServerContext dbContext = ServerContext.Create(this.Client.DbConnectionString); if (!string.IsNullOrEmpty(e.TrimmedMessage)) { string expression = e.TrimmedMessage.ToLower(); SocketUser user = e.Message.MentionedUsers.FirstOrDefault(); if (user == null) { user = e.Server.Guild.Users.FirstOrDefault(u => (u?.Username != null && u.Username.ToLower() == expression) || (u?.Nickname != null && u.Nickname.ToLower() == expression)); } if (user == null) { await e.SendReplySafe(NotFoundString); } else if (user.Id == this.Client.DiscordClient.CurrentUser.Id) { await e.Channel.SendMessageAsync("", embed : GetBotwinderEmbed(user as SocketGuildUser)); } else { await e.Channel.SendMessageAsync("", embed : GetProfileEmbed(dbContext, e.Server, user as SocketGuildUser)); } } else { await e.Channel.SendMessageAsync("", embed : GetProfileEmbed(dbContext, e.Server, e.Message.Author as SocketGuildUser)); } dbContext.Dispose(); }; commands.Add(newCommand); // !getProfile newCommand = new Command("getProfile"); newCommand.Type = CommandType.Standard; newCommand.Description = "Get the source used to set your profile."; newCommand.RequiredPermissions = PermissionType.Everyone; newCommand.OnExecute += async e => { if (!this.Client.IsPremium(e.Server) && !this.Client.IsTrialServer(e.Server.Id)) { await e.SendReplySafe("User profiles are a subscriber-only feature."); return; } if (!e.Server.Config.ProfileEnabled) { await e.SendReplySafe("User profiles are disabled on this server."); return; } StringBuilder response = new StringBuilder(); ServerContext dbContext = ServerContext.Create(this.Client.DbConnectionString); IEnumerable <ProfileOption> options = dbContext.ProfileOptions.Where(o => o.ServerId == e.Server.Id).OrderBy(o => o.Order); foreach (ProfileOption option in options) { UserProfileOption userOption = dbContext.UserProfileOptions.FirstOrDefault(o => o.ServerId == e.Server.Id && o.UserId == e.Message.Author.Id && o.Option == option.Option); if (userOption == null || string.IsNullOrWhiteSpace(userOption.Value)) { continue; } response.Append($"{userOption.Option} {userOption.Value} "); } response.Append("\n```"); string responseString = "There ain't no profile to get! >_<"; if (response.Length > 0) { responseString = $"```\n{e.Server.Config.CommandPrefix}setProfile {response.ToString()}"; } await e.SendReplySafe(responseString); dbContext.Dispose(); }; commands.Add(newCommand); // !setProfile newCommand = new Command("setProfile"); newCommand.Type = CommandType.Standard; newCommand.Description = "Set your profile. Get Help: setProfile --help"; newCommand.RequiredPermissions = PermissionType.Everyone; newCommand.OnExecute += async e => { if (!this.Client.IsPremium(e.Server) && !this.Client.IsTrialServer(e.Server.Id)) { await e.SendReplySafe("User profiles are a subscriber-only feature."); return; } if (!e.Server.Config.ProfileEnabled) { await e.SendReplySafe("User profiles are disabled on this server."); return; } ServerContext dbContext = ServerContext.Create(this.Client.DbConnectionString); StringBuilder response = new StringBuilder(); IEnumerable <ProfileOption> options = dbContext.ProfileOptions.Where(o => o.ServerId == e.Server.Id).OrderBy(o => o.Order); int maxOptionLength = 0; foreach (ProfileOption option in options) { if (maxOptionLength < option.OptionAlt.Length) { maxOptionLength = option.OptionAlt.Length; } } MatchCollection emptyOptions = this.ProfileEmptyOptionRegex.Matches(e.TrimmedMessage); if (string.IsNullOrEmpty(e.TrimmedMessage) || (emptyOptions.Count > 0 && emptyOptions.Cast <Match>().Any(o => o.Value == "-h" || o.Value == "--help"))) { response.AppendLine("```md\nSet your profile fields with the following parameters:"); foreach (ProfileOption o in options) { response.Append($" [ {o.Option} ][ {o.OptionAlt}"); response.Append(' ', maxOptionLength - o.OptionAlt.Length); response.AppendLine($" ] | {o.Label}"); } response.AppendLine($"\nExample:\n {e.Server.Config.CommandPrefix}{e.CommandId} --twitter @RheaAyase -y youtube.com/RheaAyase"); response.AppendLine($"\nTo null one of the options you have already set, leave it empty:\n {e.Server.Config.CommandPrefix}{e.CommandId} --twitter -y\n```"); dbContext.Dispose(); await e.SendReplySafe(response.ToString()); return; } foreach (Match match in emptyOptions) { ProfileOption option = options.FirstOrDefault(o => o.Option == match.Value || o.OptionAlt == match.Value); if (option == null) { continue; } UserProfileOption userOption = dbContext.UserProfileOptions.FirstOrDefault(o => o.ServerId == e.Server.Id && o.UserId == e.Message.Author.Id && o.Option == option.Option); if (userOption == null) { continue; } dbContext.UserProfileOptions.Remove(userOption); } MatchCollection matches = this.ProfileParamRegex.Matches(e.TrimmedMessage); foreach (Match match in matches) { string optionString = this.ProfileOptionRegex.Match(match.Value).Value; string value = match.Value.Substring(optionString.Length + 1).Replace('`', '\''); if (value.Length >= UserProfileOption.ValueCharacterLimit) { await e.SendReplySafe($"`{optionString}` is too long! (It's {value.Length} characters while the limit is {UserProfileOption.ValueCharacterLimit})"); dbContext.Dispose(); return; } ProfileOption option = options.FirstOrDefault(o => o.Option == optionString || o.OptionAlt == optionString); if (option == null) { continue; } UserProfileOption userOption = dbContext.UserProfileOptions.FirstOrDefault(o => o.ServerId == e.Server.Id && o.UserId == e.Message.Author.Id && o.Option == option.Option); if (userOption == null) { userOption = new UserProfileOption() { ServerId = e.Server.Id, UserId = e.Message.Author.Id, Option = option.Option }; dbContext.UserProfileOptions.Add(userOption); } userOption.Value = value; } dbContext.SaveChanges(); await e.Channel.SendMessageAsync("", embed : GetProfileEmbed(dbContext, e.Server, e.Message.Author as SocketGuildUser)); dbContext.Dispose(); }; commands.Add(newCommand); commands.Add(newCommand.CreateAlias("setprofile")); return(commands); }