/// <summary> /// Responds to the user with a predefined template and starts a reminder with a templated reminder text for a configurable duration. /// </summary> /// <param name="channel"><see ref="Discord.ISocketMessageChannel"> Channel of the modmail thread</param> /// <param name="moderatorUser"><see ref="Discord.WebSocket.SocketUser"> Moderator triggering command and target of the reminder</param> /// <param name="message"><see ref="Discord.WebSocket.SocketMessage"> Message triggering the command.</param> /// <returns></returns> public async Task RespondWithUsernameTemplateAndSetReminder(ISocketMessageChannel channel, SocketUser moderatorUser, SocketMessage message) { var text = Extensions.GetTemplatedString(ResponseTemplate.ILLEGAL_NAME_RESPONSE, new object[0]); var thread = ModMailManager.GetOpenModmail(channel); var user = Global.Bot.GetUser(thread.UserId); var reminderText = Extensions.GetTemplatedString(ResponseTemplate.ILLEGAL_NAME_REMINDER, new object[1] { Extensions.FormatUserName(user) }); var reponseMessageInModmailThread = await new ModMailManager().CreateModeratorReply(message, channel, moderatorUser, text, false, false); ulong seconds = 0; using (var db = new Database()) { var point = db.PersistentData.First(entry => entry.Name == "username_reminder_duration"); seconds = point.Value; var targetTime = DateTime.Now.AddSeconds(seconds); // TODO move to builder var reminder = new Reminder(); reminder.RemindText = Extensions.SanitizeIllegalPings(reminderText); reminder.RemindedUserId = moderatorUser.Id; reminder.TargetDate = targetTime; reminder.ReminderDate = DateTime.Now; reminder.ChannelId = channel.Id; reminder.MessageId = reponseMessageInModmailThread.Id; db.Reminders.Add(reminder); db.SaveChanges(); if (targetTime <= DateTime.Now.AddMinutes(60)) { var difference = targetTime - DateTime.Now; ReminderTimerManger.RemindUserIn(moderatorUser.Id, difference, reminder.ID); reminder.ReminderScheduled = true; } db.SaveChanges(); } }
/// <summary> /// Handles the user reply: updates the state in the db, posts the message to the modmail thread and mentions subscribers /// </summary> /// <param name="message">The <see chref="Discord.WebSocket.SocketMessage"> message object coming from the user</param> /// <returns></returns> public async Task HandleModMailUserReply(SocketMessage message) { var bot = Global.Bot; var guild = bot.GetGuild(Global.ServerID); var modMailThread = ModMailManager.GetOpenModmailForUser(message.Author); if (modMailThread != null) { UpdateModThreadUpdatedField(modMailThread.ChannelId, "USER_REPLIED"); var mentions = new StringBuilder(""); foreach (var subscriber in modMailThread.Subscriber) { var subscriberUser = guild.GetUser(subscriber.UserId); if (subscriberUser != null) { mentions.Append(subscriberUser.Mention); } } var channel = guild.GetTextChannel(modMailThread.ChannelId); if (channel != null) { var msg = await channel.SendMessageAsync(mentions.ToString(), embed : ModMailEmbedHandler.GetReplyEmbed(message)); AddModMailMessage(modMailThread.ChannelId, msg, null, message.Author.Id); } var userMsg = await message.Channel.GetMessageAsync(message.Id); if (userMsg is RestUserMessage) { var currentMessageInUserDmChannel = userMsg as RestUserMessage; await currentMessageInUserDmChannel.AddReactionAsync(new Emoji("✅")); } else if (userMsg is SocketUserMessage) { var currentMessageInUserDmChannel = userMsg as SocketUserMessage; await currentMessageInUserDmChannel.AddReactionAsync(new Emoji("✅")); } } }