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(); } }
public async Task EditMessage(string newText, ulong messageId, ISocketMessageChannel channel, SocketUser personEditing) { ThreadMessage messageToEdit; ModMailThread thread; using (var db = new Database()) { messageToEdit = db.ThreadMessages.Where(msg => msg.ChannelId == channel.Id && msg.UserId == personEditing.Id && msg.ChannelMessageId == messageId).FirstOrDefault(); thread = db.ModMailThreads.Where(th => th.ChannelId == channel.Id).First(); } if (messageToEdit == null) { throw new Exception("No message found to edit"); } 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."); } IDMChannel userDmChannel = await user.GetOrCreateDMChannelAsync(); var channelObj = guild.GetTextChannel(channel.Id); if (channelObj != null) { IMessage rawChannelMessage = await channelObj.GetMessageAsync(messageToEdit.ChannelMessageId); Embed newEmbed = rawChannelMessage.Embeds.First().ToEmbedBuilder().WithDescription(newText).Build(); // these ifs are needed, because when the message is cached it remains a socket message, when its older (or the bot was restarted) its a restmessage if (rawChannelMessage is RestUserMessage) { var currentMessageInChannel = rawChannelMessage as RestUserMessage; await currentMessageInChannel.ModifyAsync(msg => msg.Embed = newEmbed); } else if (rawChannelMessage is SocketUserMessage) { var currentMessageInChannel = rawChannelMessage as SocketUserMessage; await currentMessageInChannel.ModifyAsync(msg => msg.Embed = newEmbed); } IMessage rawDmMessage = await userDmChannel.GetMessageAsync(messageToEdit.UserMessageId); if (rawDmMessage is RestUserMessage) { var currentMessageInUserDmChannel = rawDmMessage as RestUserMessage; await currentMessageInUserDmChannel.ModifyAsync(msg => msg.Embed = newEmbed); } else if (rawDmMessage is SocketUserMessage) { var currentMessageInUserDmChannel = rawDmMessage as SocketUserMessage; await currentMessageInUserDmChannel.ModifyAsync(msg => msg.Embed = newEmbed); } } }
public static async Task <IMessage> GetMessage(ulong channelID, ulong messageID) { SocketTextChannel channel = Utility.GetServer().GetChannel(channelID) as SocketTextChannel; if (channel != null) { return(await channel.GetMessageAsync(messageID)); } SocketUser user = Utility.GetServer().GetUser(messageID); if (user != null) { IDMChannel userChannel = await user.GetOrCreateDMChannelAsync(); return(await userChannel.GetMessageAsync(messageID)); } return(null); }
public async Task EndVoteTask(string guidString) { JObject temp = await db.getJObjectAsync(guidString, "votes"); ShareholderVote vote = new ShareholderVote(temp); //vote.JSON(await db.getJObjectAsync(guidString, "votes")); Dictionary <ulong, string> votes = new Dictionary <ulong, string>(); // user, vote foreach (ulong ID in vote.messages.Keys) { IDMChannel dmChannel = await Context.Client.GetDMChannelAsync(ID); IUserMessage message = (IUserMessage)await dmChannel.GetMessageAsync(vote.messages[ID]); Collection <IEmote> reactionsRaw = (Collection <IEmote>)message.Reactions.Keys; Collection <IEmote> reactions = new Collection <IEmote>(); foreach (IEmote reaction in reactionsRaw) { } } }
private async Task DisplayPlayer(Player player) { try { // Get the channel for this adventure.. SocketUser user = DiscordSocketClient.GetUser(Convert.ToUInt64(player.Id)); IDMChannel channel = await user.GetOrCreateDMChannelAsync(); // Get the adventureMessage that we will update. IUserMessage playerSheetMessage; // If we were passed the "RegenerateMessages" flag set to true, or if there is no PlayerSheetMessage, then create a new adventureMessage. if (player.RegenerateMessage || player.PlayerSheetMessage == 0) { playerSheetMessage = await channel.SendMessageAsync("<PLAYER SHEET MESSAGE>") as IUserMessage; } // If we were passed the "RegenerateMessages" flag set to false, then retrieve the existing adventureMessage. else { playerSheetMessage = await channel.GetMessageAsync(player.PlayerSheetMessage) as IUserMessage; } // Alter the messages to now have new status updates. EmbedBuilder embedBuilder = new EmbedBuilder(); embedBuilder.Color = Color.Blue; string adventureHeader = $"PLAYER SHEET"; embedBuilder.Title = adventureHeader; if (player.GameStatus == Player.PlayerGameStatus.Adventure) { double healthTicksRaw = player.Health / (double)player.MaxHealth; int healthTicks = 0; for (int i = 1; i < BarDisplaySize + 1; i++) { if (healthTicksRaw < (i / (double)BarDisplaySize)) { break; } healthTicks += 1; } string healthBar = "["; for (int i = 0; i < healthTicks; i++) { healthBar += "+"; } for (int i = 0; i < BarDisplaySize - healthTicks; i++) { healthBar += "-"; } healthBar += "]"; double manaTicksRaw = player.Mana / (double)Player.MaxMana; int manaTicks = 0; for (int i = 1; i < BarDisplaySize + 1; i++) { if (manaTicksRaw < (i / (double)BarDisplaySize)) { break; } manaTicks += 1; } string manaBar = "["; for (int i = 0; i < manaTicks; i++) { manaBar += "+"; } for (int i = 0; i < BarDisplaySize - manaTicks; i++) { manaBar += "-"; } manaBar += "]"; embedBuilder.AddField("Class", player.Class == null ? "NOT SET!" : player.Class.ToString()); embedBuilder.AddField("Health Points", $"{player.Health}/{player.MaxHealth} {healthBar}"); embedBuilder.AddField("Mana Points", $"{player.Mana}/{Player.MaxMana} {manaBar}"); string timePassed = (player.AdventureJoinTime == null) ? "" : (DateTime.UtcNow - player.AdventureJoinTime.Value).ToString(@"mm\:ss"); embedBuilder.AddField("Status", $"On Adventure ({timePassed})"); } else if (player.GameStatus == Player.PlayerGameStatus.Idle) { embedBuilder.AddField("Class", player.Class == null ? "NOT SET!" : player.Class.ToString()); embedBuilder.AddField("Health Points", $"{player.MaxHealth}/{player.MaxHealth}"); embedBuilder.AddField("Mana Points", $"{Player.MaxMana}/{Player.MaxMana}"); embedBuilder.AddField("Status", "Idle"); } else { throw new Exception("Invalid Player Game Status!"); } await playerSheetMessage.ModifyAsync(m => { m.Content = ""; m.Embed = embedBuilder.Build(); }); // If we were passed the "RegenerateMessages" flag set to true, or if there was no old PlayerSheetMessage, then delete old adventureMessage if possible. // Then, toggle the "RegenerateMessages" flag to false, and set the new adventureMessage on the Adventure. if (player.RegenerateMessage || player.PlayerSheetMessage == 0) { if (player.PlayerSheetMessage != 0) { await channel.DeleteMessageAsync(await channel.GetMessageAsync(player.PlayerSheetMessage)); } await PlayersRepository.SetNewChannelsForMessages ( playerId : player.Id, playerSheetMessage : playerSheetMessage.Id ); } } catch (Exception e) { Console.WriteLine(e); } }