コード例 #1
0
        /// <summary>
        /// Builds the embed used to inform the moderator about the user
        /// </summary>
        /// <param name="thread">The newly created modmail thread</param>
        /// <returns><see cref="Discord.Embed"> object containing information about the user</returns>
        public static Embed GetUserInfoHeader(ModMailThread thread)
        {
            var    builder    = GetBaseEmbed();
            var    bot        = Global.Bot;
            var    guild      = bot.GetGuild(Global.ServerID);
            var    user       = guild.GetUser(thread.UserId);
            string joinedPart = "";

            if (user.JoinedAt.HasValue)
            {
                joinedPart = $"Join date: {Extensions.FormatDateTime(user.JoinedAt.Value.DateTime)} ";
            }
            StringBuilder rolesPart = new StringBuilder();

            foreach (var role in user.Roles)
            {
                rolesPart.Append(role.Name);
                if (role != user.Roles.Last())
                {
                    rolesPart.Append(", ");
                }
            }
            if (user.Roles.Count() == 0)
            {
                rolesPart.Append("no roles");
            }
            builder.WithAuthor(GetOneplusAuthor());
            builder.WithDescription($"User: {Extensions.FormatUserNameDetailed(user)}: {joinedPart} {user.Nickname} \n User has the following roles: {rolesPart.ToString()}");
            return(builder.Build());
        }
コード例 #2
0
        private async Task DeleteMessageInThread(ModMailThread thread, ThreadMessage message, bool deleteMessage = true)
        {
            var bot   = Global.Bot;
            var guild = bot.GetGuild(Global.ServerID);
            var user  = guild.GetUser(thread.UserId);

            if (user == null)
            {
                throw new Exception("User was not found. Probably left the guild.");
            }
            var        channelObj    = guild.GetTextChannel(thread.ChannelId);
            IDMChannel userDmChannel = await user.GetOrCreateDMChannelAsync();

            IMessage rawChannelMessage = await channelObj.GetMessageAsync(message.ChannelMessageId);

            if (deleteMessage)
            {
                await rawChannelMessage.DeleteAsync();
            }

            IMessage rawDmMessage = await userDmChannel.GetMessageAsync(message.UserMessageId);

            await rawDmMessage.DeleteAsync();

            using (var db = new Database()){
                db.ThreadMessages.Remove(message);
                db.SaveChanges();
            }
        }
コード例 #3
0
ファイル: ModMailManager.cs プロジェクト: Rithari/OnePlusBot
        /// <summary>
        /// Retrieves the interactions between the moderators and staff, calls the methods reponsible for logging the interactions and deletes the channel
        /// </summary>
        /// <param name="closedThread">The <see cref"OnePlusBot.Data.Models.ModMailThread"/> object of the thread being closed</param>
        /// <param name="channel">The <see cref"Discord.WebSocket.ISocketMessageChannel"/> object in which the modmail interactions happened</param>
        /// <param name="note">Optional note used when closing the thread</param>
        /// <param name="silent">Whether or not the thread was closed silently</param>
        /// <returns>Task containing the number of logged messages.</returns>
        private async Task <int> DeleteChannelAndLogThread(ModMailThread closedThread, ISocketMessageChannel channel, string note, Boolean silent)
        {
            var                  bot     = Global.Bot;
            var                  guild   = bot.GetGuild(Global.ServerID);
            SocketGuildUser      userObj = guild.GetUser(closedThread.UserId);
            List <ThreadMessage> messagesToLog;

            using (var db = new Database())
            {
                messagesToLog = db.ThreadMessages.AsQueryable().Where(ch => ch.ChannelId == closedThread.ChannelId).ToList();
            }
            var modMailLogChannel = guild.GetTextChannel(Global.PostTargets[PostTarget.MODMAIL_LOG]);

            await LogClosingHeader(closedThread, messagesToLog.Count(), note, modMailLogChannel, userObj, silent);

            await LogModMailThreadMessagesToModmailLog(closedThread, messagesToLog, modMailLogChannel);

            await(channel as SocketTextChannel).DeleteAsync();
            using (var db = new Database())
            {
                var threadObj = db.ModMailThreads.AsQueryable().Where(ch => ch.ChannelId == channel.Id).FirstOrDefault();
                if (threadObj != null)
                {
                    threadObj.ClosedDate = DateTime.Now;
                    threadObj.State      = "CLOSED";
                }
                db.SaveChanges();
                Global.ReloadModmailThreads();
            }
            return(messagesToLog.Count());
        }
コード例 #4
0
ファイル: ModMailManager.cs プロジェクト: Rithari/OnePlusBot
        /// <summary>
        /// Creates the channel containing the modmail thread and creates the thread in the database
        /// </summary>
        /// <param name="targetUser">The <see cref="Discord.IUser"> user to create the modmail thread for</param>
        /// <returns>The <see chref="Discord.Rest.RestTextChannel"> newly created channel</returns>
        private static async Task <RestTextChannel> CreateModMailThread(IUser targetUser)
        {
            var bot             = Global.Bot;
            var guild           = bot.GetGuild(Global.ServerID);
            var modmailCategory = guild.GetCategoryChannel(Global.ModmailCategoryId);
            var timeStamp       = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            var md5             = new MD5CryptoServiceProvider();

            byte[] textToHash = Encoding.Default.GetBytes(timeStamp.ToString());
            byte[] result     = md5.ComputeHash(textToHash);

            var hashAsText = System.BitConverter.ToString(result).Replace("-", string.Empty);
            var channel    = await guild.CreateTextChannelAsync(hashAsText.Substring(0, 6), (TextChannelProperties prop) => {
                prop.CategoryId = modmailCategory.Id;
            });

            var thread = new ModMailThread();

            thread.CreateDate = DateTime.Now;
            thread.ChannelId  = channel.Id;
            thread.UserId     = targetUser.Id;
            thread.State      = "INITIAL";
            using (var db = new Database())
            {
                db.ModMailThreads.Add(thread);
                db.SaveChanges();
            }
            Global.ReloadModmailThreads();
            return(channel);
        }
コード例 #5
0
        public static Embed GetClosingSummaryEmbed(ModMailThread thread, int messageCount, SocketUser user, string note)
        {
            var embed = GetBaseEmbed();

            embed.WithTitle("Modmail thread has been closed");
            embed.WithDescription(GetClosingHeader(thread, messageCount, user, note).ToString());
            return(embed.Build());
        }
コード例 #6
0
        public static Embed GetClosingSummaryEmbed(ModMailThread thread, int messageCount, SocketUser user, string note, Boolean silent)
        {
            var embed        = GetBaseEmbed();
            var silentSuffix = silent ? " (silently)" : "";

            embed.WithTitle($"Modmail thread has been closed{silentSuffix}.");
            embed.WithDescription(GetClosingHeader(thread, messageCount, user, note).ToString());
            return(embed.Build());
        }
コード例 #7
0
        private static StringBuilder GetClosingHeader(ModMailThread thread, int messageCount, SocketUser user, string note)
        {
            var descriptionBuilder = new StringBuilder();

            descriptionBuilder.Append($"A modmail thread has been closed with the note '{note}' \n ");
            descriptionBuilder.Append($"There were {messageCount} interactions with the user {Extensions.FormatUserNameDetailed(user)}. \n");
            descriptionBuilder.Append($"It has been opened on {thread.CreateDate:dd.MM.yyyy HH:mm} {TimeZoneInfo.Local}");
            descriptionBuilder.Append($" and lasted {Extensions.FormatTimeSpan(DateTime.Now - thread.CreateDate)}.");
            return(descriptionBuilder);
        }
コード例 #8
0
        public static Embed GetModqueueNotificationEmbed(SocketUser user, ModMailThread thread)
        {
            var builder = GetBaseEmbed();

            builder.WithAuthor(GetUserAuthor(user));
            builder.WithTitle("A new modmail thread has been opened");
            builder.WithDescription($"The thread concerns {Extensions.FormatUserNameDetailed(user)}.");
            builder.AddField("Link", Extensions.GetChannelUrl(Global.ServerID, thread.ChannelId, "Jump!"));
            return(builder.Build());
        }
コード例 #9
0
        /// <summary>
        /// Returns the string containing the given parameters formatted as the header used in the 'ModMailLog' embed which is used as the header
        /// </summary>
        /// <param name="thread">The <see cref"OnePlusBot.Data.Models.ModMailThread"/> object which is being closed.</param>
        /// <param name="messageCount">The amount of interactions between the user and the staff</param>
        /// <param name="user">The <see cref"Discord.WebSocket.SocketUser"/> object for who the thread was created</param>
        /// <param name="note">An optional note which is displayed as information in the closing header</param>
        /// <returns>The StringBuilder object containing the formatted information used as closing header.</returns>
        private static StringBuilder GetClosingHeader(ModMailThread thread, int messageCount, SocketUser user, string note)
        {
            var    descriptionBuilder = new StringBuilder();
            string defaultedNote      = note ?? "No note";

            descriptionBuilder.Append($"A modmail thread has been closed with the note '**{ defaultedNote }**' \n ");
            descriptionBuilder.Append($"There were {messageCount} interactions with the user **{Extensions.FormatUserName(user)}** ({thread.UserId}). \n");
            descriptionBuilder.Append($"It has been opened on {Extensions.FormatDateTime(thread.CreateDate)}");
            descriptionBuilder.Append($" and lasted {Extensions.FormatTimeSpan(DateTime.Now - thread.CreateDate)}.");
            return(descriptionBuilder);
        }
コード例 #10
0
        public static Embed GetThreadHasBeendCreatedEmbed(ModMailThread thread)
        {
            var builder = GetBaseEmbed();
            var bot     = Global.Bot;
            var guild   = bot.GetGuild(Global.ServerID);
            var user    = guild.GetUser(thread.UserId);

            builder.WithDescription("Thread has been created.");
            builder.AddField("Link", Extensions.GetChannelUrl(guild.Id, thread.ChannelId, user.Username + user.Discriminator));
            return(builder.Build());
        }
コード例 #11
0
        public static Embed GetMutingSummaryEmbed(ModMailThread thread, int messageCount, SocketUser user, string note, DateTime until)
        {
            StringBuilder description = GetClosingHeader(thread, messageCount, user, note);

            description.Append($"\n It has been disabled and will be available again at {until:dd.MM.yyyy HH:mm} {TimeZoneInfo.Local}.");
            var embed = GetBaseEmbed();

            embed.WithTitle("Modmail thread has been disabled for user");
            embed.WithDescription(description.ToString());
            return(embed.Build());
        }
コード例 #12
0
        /// <summary>
        /// Builds the header for the log when the thread has been closed and disabled for a certain timeperiod
        /// </summary>
        /// <param name="thread">The <see cref="OnePlusBot.Data.Models.ModMailThread"> being closed</param>
        /// <param name="messageCount">The amount of messages between the moderators and the user</param>
        /// <param name="user">The <see cref="Discord.WebSocket.SocketUser"> object for which the thread was opened</param>
        /// <param name="note">The note (optional) which was used to close the thread</param>
        /// <param name="until">The <see cref="System.DateTime"> at which modmail will be available again for the user</param>
        /// <returns>The <see cref="Discord.Embed"> containing the information</returns>
        public static Embed GetMutingSummaryEmbed(ModMailThread thread, int messageCount, SocketUser user, string note, DateTime until)
        {
            StringBuilder description = GetClosingHeader(thread, messageCount, user, note);

            description.Append($"\n It has been disabled and will be available again on {Extensions.FormatDateTime(until)}.");
            var embed = GetBaseEmbed();

            embed.WithTitle("Modmail thread has been disabled for user.");
            embed.WithDescription(description.ToString());
            return(embed.Build());
        }
コード例 #13
0
        private async Task LogModMailThreadMessagesToModmailLog(ModMailThread modMailThread, string note, List <ThreadMessage> messagesToLog, SocketTextChannel targetChannel)
        {
            var bot     = Global.Bot;
            var guild   = bot.GetGuild(Global.ServerID);
            var channel = guild.GetTextChannel(modMailThread.ChannelId);

            foreach (var msg in messagesToLog)
            {
                var msgToLog = await channel.GetMessageAsync(msg.ChannelMessageId);

                var messageUser = bot.GetGuild(Global.ServerID).GetUser(msg.UserId);
                var msgText     = msg.Anonymous && messageUser != null?Extensions.FormatUserNameDetailed(messageUser) : "";

                await targetChannel.SendMessageAsync(msgText, embed : msgToLog.Embeds.First().ToEmbedBuilder().Build());

                await Task.Delay(500);
            }
        }
コード例 #14
0
        /// <summary>
        /// Retrieves the interactions between the moderators and staff, calls the methods reponsible for logging the interactions and deletes the channel
        /// </summary>
        /// <param name="closedThread">The <see cref"OnePlusBot.Data.Models.ModMailThread"/> object of the thread being closed</param>
        /// <param name="channel">The <see cref"Discord.WebSocket.ISocketMessageChannel"/> object in which the modmail interactions happened</param>
        /// <param name="note">Optional note used when closing the thread</param>
        /// <returns>Task containing the number of logged messages.</returns>
        private async Task <int> DeleteChannelAndLogThread(ModMailThread closedThread, ISocketMessageChannel channel, string note)
        {
            var                  bot     = Global.Bot;
            var                  guild   = bot.GetGuild(Global.ServerID);
            SocketGuildUser      userObj = guild.GetUser(closedThread.UserId);
            List <ThreadMessage> messagesToLog;

            using (var db = new Database())
            {
                messagesToLog = db.ThreadMessages.Where(ch => ch.ChannelId == closedThread.ChannelId).ToList();
            }
            var modMailLogChannel = guild.GetTextChannel(Global.PostTargets[PostTarget.MODMAIL_LOG]);

            await LogClosingHeader(closedThread, messagesToLog.Count(), note, modMailLogChannel, userObj);

            await LogModMailThreadMessagesToModmailLog(closedThread, messagesToLog, modMailLogChannel);

            await(channel as SocketTextChannel).DeleteAsync();
            return(messagesToLog.Count());
        }
コード例 #15
0
        private async Task <RestTextChannel> CreateModMailThread(IUser targetUser)
        {
            var bot             = Global.Bot;
            var guild           = bot.GetGuild(Global.ServerID);
            var modmailCategory = guild.GetCategoryChannel(Global.ModmailCategoryId);
            var channel         = await guild.CreateTextChannelAsync(targetUser.Username + targetUser.Discriminator, (TextChannelProperties prop) => {
                prop.CategoryId = modmailCategory.Id;
            });

            var thread = new ModMailThread();

            thread.CreateDate = DateTime.Now;
            thread.ChannelId  = channel.Id;
            thread.UserId     = targetUser.Id;
            thread.State      = "INITIAL";
            using (var db = new Database())
            {
                db.ModMailThreads.Add(thread);
                db.SaveChanges();
            }
            Global.ReloadModmailThreads();
            return(channel);
        }
コード例 #16
0
ファイル: ModMailManager.cs プロジェクト: Rithari/OnePlusBot
 private async Task LogClosingHeader(ModMailThread modMailThread, int messageCount, string note, SocketTextChannel modMailLogChannel, SocketUser modmailUser, Boolean silent)
 {
     var closingEmbed = ModMailEmbedHandler.GetClosingSummaryEmbed(modMailThread, messageCount, modmailUser, note, silent);
     await modMailLogChannel.SendMessageAsync(embed : closingEmbed);
 }
コード例 #17
0
 private async Task LogDisablingHeader(ModMailThread modMailThread, int messageCount, string note, SocketTextChannel modMailLogChannel, SocketUser modmailUser, DateTime until)
 {
     var closingEmbed = ModMailEmbedHandler.GetMutingSummaryEmbed(modMailThread, messageCount, modmailUser, note, until);
     await modMailLogChannel.SendMessageAsync(embed : closingEmbed);
 }