private async Task ShardOnMessageDeleted(Cacheable <IMessage, ulong> cachedMessage, ISocketMessageChannel textChannel) { GuildBson guild = await this.database.LoadRecordsByGuildId(((SocketTextChannel)textChannel).Guild.Id); if (!GetRestTextChannel(this.shard, guild.MessageDeletedEvent, out RestTextChannel restTextChannel)) { return; } IMessage message = cachedMessage.Value; EmbedBuilder embedBuilder = new() { Author = new EmbedAuthorBuilder { Name = message.Author.Mention, IconUrl = message.Author.GetAvatarUrl() }, Title = $"Message by {message.Author.Mention} was deleted in {((SocketTextChannel) textChannel).Mention}", Description = message.Content, Footer = new EmbedFooterBuilder { Text = $"Author ID: {message.Author.Id}, Message ID: {message.Id}, at {DateTime.UtcNow} UTC" } }; await restTextChannel.SendMessageAsync("", false, embedBuilder.Build()); } }
public async Task CreateSection() { GuildBson guild = await Database.LoadRecordsByGuildId(Context.Guild.Id); RestCategoryChannel categoryChannel = await Context.Guild.CreateCategoryChannelAsync("Auditor"); await categoryChannel.AddPermissionOverwriteAsync(Context.Guild.EveryoneRole, OverwritePermissions.DenyAll(categoryChannel)); foreach ((string name, ulong?channel) in GetSettingStates(guild)) { if (channel == null) { continue; } RestTextChannel textChannel = await Context.Guild.CreateTextChannelAsync(name.Replace("Event", ""), o => o.CategoryId = categoryChannel.Id); guild.GetType().GetProperty(name)?.SetValue(guild, textChannel.Id); } guild.CategoryId = categoryChannel.Id; await Database.UpdateGuild(guild); await SendSuccessAsync("Created Auditor sections, please move to a more convenient place."); }
public async Task DeleteSection() { GuildBson guild = await Database.LoadRecordsByGuildId(Context.Guild.Id); if (guild.CategoryId != null) { SocketCategoryChannel categoryChannel = Context.Guild.GetCategoryChannel(guild.CategoryId.Value); foreach (SocketGuildChannel channel in categoryChannel.Channels) { await channel.DeleteAsync(); } foreach (PropertyInfo info in guild.GetType().GetProperties() .Where(c => c.PropertyType == typeof(ulong?) && c.GetValue(guild) != null)) { guild.GetType().GetProperty(info.Name)?.SetValue(guild, (ulong?)1); } await categoryChannel.DeleteAsync(); } guild.CategoryId = null; await Database.UpdateGuild(guild); await SendSuccessAsync("Deleted Auditor sections."); }
// Loading Records public async Task <GuildBson> LoadRecordsByGuildId(ulong guildId) { if (_matsueGuildCache.TryGetValue(guildId, out var cacheGuild)) { return(cacheGuild); } var collection = _mongoDatabase.GetCollection <GuildBson>("guilds"); var filter = Builders <GuildBson> .Filter.Eq("guild_id", guildId); GuildBson guild; try { guild = await collection.Find(filter).FirstAsync(); } catch { guild = new GuildBson { Prefix = ";", GuildId = guildId }; await InsertRecord("guilds", guild); } _matsueGuildCache.Add(guild.GuildId, guild); return(guild); }
private async Task ShardOnUserLeft(SocketGuildUser user) { GuildBson guild = await this.database.LoadRecordsByGuildId(user.Guild.Id); if (GetRestTextChannel(this.shard, guild.UserLeftEvent, out RestTextChannel restTextChannel)) { List <EmbedFieldBuilder> fields = new() { new EmbedFieldBuilder { Name = "Username", Value = user.Username }, new EmbedFieldBuilder { Name = "User Id", Value = user.Id } }; EmbedBuilder embedBuilder = new() { Color = Color.Blue, Fields = fields, ImageUrl = user.GetAvatarUrl(), Footer = new EmbedFooterBuilder { Text = $"Left on {DateTime.UtcNow} UTC" } }; await restTextChannel.SendMessageAsync("", false, embedBuilder.Build()); } } } }
private async Task ShardOnMessagesBulkDeleted(IReadOnlyCollection <Cacheable <IMessage, ulong> > cachedMessages, ISocketMessageChannel textChannel) { GuildBson guild = await this.database.LoadRecordsByGuildId(((SocketTextChannel)textChannel).Guild.Id); if (GetRestTextChannel(this.shard, guild.MessagesBulkDeletedEvent, out RestTextChannel restTextChannel)) { List <EmbedBuilder> pages = new(); foreach (Cacheable <IMessage, ulong> message in cachedMessages) { pages.Add(new EmbedBuilder { Author = new EmbedAuthorBuilder { Name = message.Value.Author.Mention, IconUrl = message.Value.Author.GetAvatarUrl() }, Description = message.Value.Content, Footer = new EmbedFooterBuilder { Text = $"Author ID: {message.Value.Author.Id}, at {DateTime.UtcNow} UTC" } }); } PaginatedMessage paginatedMessage = new(pages, $"Bulk Delete from {((SocketTextChannel) textChannel).Mention}", Color.Blue, null, new AppearanceOptions { Style = DisplayStyle.Minimal }); await this.paginationService.SendMessageAsync(restTextChannel, paginatedMessage); } }
private async Task OnGuildUpdated(SocketGuild prevGuild, SocketGuild newGuild) { GuildBson guild = await this.database.LoadRecordsByGuildId(prevGuild.Id); if (GetRestTextChannel(this.shard, guild.GuildUpdatedEvent, out RestTextChannel restTextChannel)) { List <EmbedFieldBuilder> fields = new(); foreach (PropertyInfo info in EnumeratingUtilities.GetDifferentProperties(prevGuild, newGuild, new[] { "" })) { fields.Add(new EmbedFieldBuilder { Name = info.Name, Value = $"{info.GetValue(prevGuild) ?? "null"} to {info.GetValue(newGuild) ?? "null"}" }); } EmbedBuilder embedBuilder = new() { Color = Color.Blue, Fields = fields, Footer = new EmbedFooterBuilder { Text = $"Modified at {DateTime.UtcNow} UTC" } }; await restTextChannel.SendMessageAsync("", false, embedBuilder.Build()); } } }
public async Task ToggleMultiple(params Events[] eventToggle) { GuildBson guild = await Database.LoadRecordsByGuildId(Context.Guild.Id); foreach (Events e in eventToggle) { PropertyInfo info = typeof(GuildBson).GetProperties().FirstOrDefault(o => string.Equals(o.Name, e + "Event", StringComparison.InvariantCultureIgnoreCase)); if (info == null) { await SendErrorAsync($"Failed getting {e}, make sure you type the right event name"); } else { if (info.GetValue(guild) != null) { guild.GetType().GetProperty(info.Name)?.SetValue(guild, null); } else { guild.GetType().GetProperty(info.Name)?.SetValue(guild, (ulong?)1); } await Database.UpdateGuild(guild); } } }
private async Task ShardOnMessageReceived(SocketMessage message) { if (message.Source == MessageSource.Bot) { return; } GuildBson guild = await this.database.LoadRecordsByGuildId(((SocketTextChannel)message.Channel).Guild.Id); if (GetRestTextChannel(this.shard, guild.MessageReceivedEvent, out RestTextChannel restTextChannel)) { EmbedBuilder embedBuilder = new() { Author = new EmbedAuthorBuilder { Name = message.Author.Mention, IconUrl = message.Author.GetAvatarUrl(), Url = message.GetJumpUrl() }, Title = $"Message Sent in #{message.Channel.Name} | {message.Channel.Id}", Description = message.Content, Color = Color.Blue, Footer = new EmbedFooterBuilder { Text = $"User ID: {message.Author.Id}, Message ID: {message.Id}, sent at {DateTime.UtcNow} UTC" } }; await restTextChannel.SendMessageAsync("", false, embedBuilder.Build()); } } }
private async Task ShardOnReactionAdded(Cacheable <IUserMessage, ulong> message, ISocketMessageChannel textChannel, SocketReaction reaction) { if (reaction.User.Value.IsBot) { return; } GuildBson guild = await this.database.LoadRecordsByGuildId(((SocketTextChannel)textChannel).Guild.Id); if (GetRestTextChannel(this.shard, guild.ReactionAddedEvent, out RestTextChannel restTextChannel)) { EmbedBuilder embedBuilder = new() { Title = $"{reaction.User.Value.Mention} added a reaction", Description = $"{reaction.Emote.Name} was added to Message ID: {message.Value.Id} in {message.Value.Channel.Name}", Color = Color.Blue, Footer = new EmbedFooterBuilder { Text = $"User ID: {reaction.UserId}, Message ID {message.Value.Id}, added at {DateTime.UtcNow} UTC" } }; await restTextChannel.SendMessageAsync("", false, embedBuilder.Build()); } } }
private async Task ShardOnRoleDeleted(SocketRole role) { GuildBson guild = await this.database.LoadRecordsByGuildId(role.Guild.Id); if (GetRestTextChannel(this.shard, guild.RoleDeletedEvent, out RestTextChannel restTextChannel)) { string permissions = role.Permissions.ToList() .Aggregate("", (current, permission) => current + permission + ", "); EmbedBuilder embedBuilder = new() { Color = role.Color, Fields = new List <EmbedFieldBuilder> { new() { Name = "Role", Value = role.Name }, new() { Name = "Permissions", Value = permissions } }, Footer = new EmbedFooterBuilder { Text = $"Deleted at {DateTime.UtcNow} UTC" } }; await restTextChannel.SendMessageAsync("", false, embedBuilder.Build()); } }
// Updating Records public async Task UpdateGuild(GuildBson record) { _matsueGuildCache[record.GuildId] = record; var collection = _mongoDatabase.GetCollection <GuildBson>("guilds"); var filter = Builders <GuildBson> .Filter.Eq("guild_id", record.GuildId); await collection.ReplaceOneAsync(filter, record); }
public async Task Prefix(string prefix) { GuildBson guild = await Database.LoadRecordsByGuildId(Context.Guild.Id); guild.Prefix = prefix; await Database.UpdateGuild(guild); await SendSuccessAsync("Prefix Updated"); }
private async Task ShardOnMessageUpdated(Cacheable <IMessage, ulong> cachedMessage, SocketMessage newMessage, ISocketMessageChannel textChannel) { if (cachedMessage.Value.Author.IsBot) { return; } GuildBson guild = await this.database.LoadRecordsByGuildId(((SocketTextChannel)textChannel).Guild.Id); if (GetRestTextChannel(this.shard, guild.MessageUpdatedEvent, out RestTextChannel restTextChannel)) { List <EmbedBuilder> pages = new(); pages.Add(new EmbedBuilder { Author = new EmbedAuthorBuilder { Name = newMessage.Author.Mention, IconUrl = newMessage.Author.GetAvatarUrl(), Url = newMessage.GetJumpUrl() }, Title = "Message Updated: Before", Description = cachedMessage.Value.Content, Footer = new EmbedFooterBuilder { Text = $"Author ID: {newMessage.Author.Id}, Message ID: {newMessage.Id}, created at {cachedMessage.Value.Timestamp.UtcDateTime} UTC" } }); pages.Add(new EmbedBuilder { Author = new EmbedAuthorBuilder { Name = newMessage.Author.Mention, IconUrl = newMessage.Author.GetAvatarUrl(), Url = newMessage.GetJumpUrl() }, Title = "Message Updated: After", Description = newMessage.Content, Footer = new EmbedFooterBuilder { Text = $"Author ID: {newMessage.Author.Id}, Message ID: {newMessage.Id}, updated at {DateTime.UtcNow} UTC" } }); PaginatedMessage paginatedMessage = new(pages, "Message Updated", Color.Blue, null, new AppearanceOptions { Style = DisplayStyle.Minimal }); await this.paginationService.SendMessageAsync(restTextChannel, paginatedMessage); } }
private async Task ShardOnUserVoiceStateUpdated(SocketUser user, SocketVoiceState prevVoiceState, SocketVoiceState newVoiceState) { ulong?guildId = prevVoiceState.VoiceChannel?.Guild.Id ?? newVoiceState.VoiceChannel?.Guild.Id; GuildBson guild = await this.database.LoadRecordsByGuildId(guildId.Value); if (GetRestTextChannel(this.shard, guild.UserVoiceStateUpdatedEvent, out RestTextChannel restTextChannel)) { List <EmbedFieldBuilder> fields = new(); if (prevVoiceState.VoiceChannel != null) { fields.Add(new EmbedFieldBuilder { Name = $"Left {prevVoiceState.VoiceChannel.Name}", Value = DateTime.UtcNow + " UTC" }); } if (newVoiceState.VoiceChannel != null) { fields.Add(new EmbedFieldBuilder { Name = $"Joined {newVoiceState.VoiceChannel.Name}", Value = DateTime.UtcNow + " UTC" }); } EmbedBuilder embedBuilder = new() { Title = user.Username, Fields = fields, Color = Color.Blue, ThumbnailUrl = user.GetAvatarUrl() }; await restTextChannel.SendMessageAsync("", false, embedBuilder.Build()); } } }
private async Task ShardOnUserBanned(SocketUser user, SocketGuild socketGuild) { GuildBson guild = await this.database.LoadRecordsByGuildId(socketGuild.Id); if (GetRestTextChannel(this.shard, guild.UserBannedEvent, out RestTextChannel restTextChannel)) { RestBan ban = await socketGuild.GetBanAsync(user.Id); List <EmbedFieldBuilder> fields = new() { new EmbedFieldBuilder { Name = "Username", Value = user.Username }, new EmbedFieldBuilder { Name = "User Id", Value = user.Id }, new EmbedFieldBuilder { Name = "Ban reason", Value = ban.Reason } }; EmbedBuilder embedBuilder = new() { Color = Color.Red, Fields = fields, ImageUrl = user.GetAvatarUrl(), Footer = new EmbedFooterBuilder { Text = $"Banned on {DateTime.UtcNow} UTC" } }; await restTextChannel.SendMessageAsync("", false, embedBuilder.Build()); } } } }
private async Task ShardOnReactionsCleared(Cacheable <IUserMessage, ulong> message, ISocketMessageChannel textChannel) { GuildBson guild = await this.database.LoadRecordsByGuildId(((SocketTextChannel)textChannel).Guild.Id); if (GetRestTextChannel(this.shard, guild.ReactionsClearedEvent, out RestTextChannel restTextChannel)) { EmbedBuilder embedBuilder = new() { Title = "Reactions Cleared", Description = $"All reactions from Message ID: {message.Value.Id} in {message.Value.Channel.Name} where removed.", Color = Color.Blue, Footer = new EmbedFooterBuilder { Text = $"Message ID {message.Value.Id}, removed at {DateTime.UtcNow} UTC" } }; await restTextChannel.SendMessageAsync("", false, embedBuilder.Build()); } } }
private async Task ShardOnRoleUpdated(SocketRole oldRole, SocketRole newRole) { GuildBson guild = await this.database.LoadRecordsByGuildId(oldRole.Guild.Id); if (GetRestTextChannel(this.shard, guild.RoleUpdatedEvent, out RestTextChannel restTextChannel)) { List <EmbedFieldBuilder> fields = new(); foreach (PropertyInfo info in EnumeratingUtilities.GetDifferentProperties(oldRole, newRole, new[] { "Members" })) { fields.Add(new EmbedFieldBuilder { Name = $"Old {info.Name}", Value = info.GetValue(oldRole), IsInline = true }); fields.Add(new EmbedFieldBuilder { Name = $"New {info.Name}", Value = info.GetValue(newRole), IsInline = true }); fields.Add(new EmbedFieldBuilder { Name = "|", Value = "|", IsInline = true }); } EmbedBuilder embedBuilder = new() { Color = newRole.Color, Fields = fields, Footer = new EmbedFooterBuilder { Text = $"Modified on {DateTime.UtcNow} UTC " } }; await restTextChannel.SendMessageAsync("", false, embedBuilder.Build()); } } }
private IEnumerable <(string, ulong?)> GetSettingStates(GuildBson guild) { List <string> IgnoredProperties = new List <string> { "CollectionId", "GuildId", "Prefix", "CategoryId" }; foreach (PropertyInfo setting in typeof(GuildBson).GetProperties()) { if (IgnoredProperties.Contains(setting.Name)) { continue; } ulong?val = (ulong?)setting.GetValue(guild); yield return(setting.Name, val); } }
public async Task AuditorSettings() { string output = ""; GuildBson guild = await Database.LoadRecordsByGuildId(Context.Guild.Id); foreach ((string name, ulong?channel) in GetSettingStates(guild)) { string v = channel != null ? "🟢" : "🔴"; output += $"{v} | {name.Replace("Event", "")}\n"; } EmbedBuilder embed = new() { Title = "Settings", Description = output, Color = Color.Green, Timestamp = DateTimeOffset.Now }; await SendEmbedAsync(embed.Build()); }
private async Task ShardOnGuildMemberUpdated(SocketGuildUser prevUser, SocketGuildUser newUser) { GuildBson guild = await this.database.LoadRecordsByGuildId(prevUser.Guild.Id); if (GetRestTextChannel(this.shard, guild.GuildMemberUpdatedEvent, out RestTextChannel restTextChannel)) { List <EmbedFieldBuilder> fields = new() { new EmbedFieldBuilder { Name = "User Id", Value = newUser.Id } }; IEnumerable <PropertyInfo> differentPropertyInfos = EnumeratingUtilities.GetDifferentProperties(prevUser, newUser, new[] { "MutualGuilds", "Roles", "JoinedAt" }); foreach (PropertyInfo info in differentPropertyInfos) { fields.Add(new EmbedFieldBuilder { Name = info.Name, Value = $"{info.GetValue(prevUser) ?? "null"} to {info.GetValue(newUser) ?? "null"}" }); } EmbedBuilder embedBuilder = new() { Color = Color.Blue, Fields = fields, Footer = new EmbedFooterBuilder { Text = $"Modified at {DateTime.UtcNow} UTC" } }; await restTextChannel.SendMessageAsync("", false, embedBuilder.Build()); } } } }
public async Task SettingsUrl() { GuildBson guild = await this.Database.LoadRecordsByGuildId(Context.Guild.Id); string json = JsonConvert.SerializeObject(guild, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); JObject jobj = JObject.Parse(json); jobj.Remove("CollectionId"); jobj.Remove("GuildId"); string url = "http://localhost:5000/?existing_data=" + jobj.ToString().Replace("\r\n", "").Replace(" ", ""); Uri uri = new(url); // } EmbedBuilder embedBuilder = new() { Author = new EmbedAuthorBuilder { Name = "Click Me", Url = uri.AbsoluteUri } }; await SendEmbedAsync(embedBuilder.Build()); }