// Clears [num] number of messages from the current text channel. public async Task ClearMessagesAsync(IGuild guild, IMessageChannel channel, IUser user, int num) { // Check usage case. if (num == 0) // Check if Delete is 0, int cannot be null. { Log("You need to specify the amount | !clear (amount) | Replace (amount) with anything", (int)LogType.Text); return; } // Check permissions. var GuildUser = await guild.GetUserAsync(user.Id); if (!GuildUser.GetPermissions(channel as ITextChannel).ManageMessages) { Log("You do not have enough permissions to manage messages", (int)LogType.Text); return; } // Delete. var messages = await channel.GetMessagesAsync((int)num + 1).FlattenAsync(); foreach (IMessage m in messages) { await channel.DeleteMessageAsync(m.Id); } // Reply with status. Log($"{user.Username} deleted {num} messages", (int)LogType.Text); }
private async Task OnActivityDisabledAsync(ActivityContainer activity) { try { IMessageChannel channel = await _client.GetChannelAsync(activity.ChannelID) as IMessageChannel; await channel.DeleteMessageAsync(activity.ActivityID); } catch { } if (activity.PlannedDate > DateTime.UtcNow) { var builder = ParseActivityContainer(activity); foreach (var userID in activity.Users.Take(Activity.GetFireteamSize(activity.ActivityType)).Skip(1)) { try { var user = await _client.Rest.GetUserAsync(userID); await user.SendMessageAsync($"Ґардіане, вашу активність було скасовано.", embed : builder.Build()); } catch { } } } }
public override async Task PerformAction(SocketReaction option) { string number = option.Emote.ToString(); if (NUMBERS.Contains(number)) { int chosenIndex = Numbers.IndexOf(number); if (chosenIndex >= 0 && chosenIndex <= 10) { // if the index is valid SocketGuildUser su = UserOptions[chosenIndex]; if (hasContext) { // If this is the type of embed that needs context await selectedActionWithContext(su, Context); // Call the context method } else { await selectedAction(su); // Call the method passed in. } EventQueueManager.RemoveEvent(this); // Remove it from the queue if (Channel == null) { return; } await Channel.DeleteMessageAsync(Message); } } }
private async Task PurgeUserMessages(SocketUser user, IMessageChannel channel, int limit = -1) { List <IMessage> to_delete = new List <IMessage>(); List <IMessage> messages = new List <IMessage>(await channel.GetMessagesAsync(50).FlattenAsync()); while (messages.Count != 0 && (limit == -1 || to_delete.Count < limit)) { foreach (IMessage message in messages) { if (message.Author.Id == user.Id) { to_delete.Add(message); if (to_delete.Count >= limit) { break; } } } messages = new List <IMessage>(await channel.GetMessagesAsync(messages[messages.Count - 1], Direction.Before, 50).FlattenAsync()); } foreach (IMessage message in to_delete) { await channel.DeleteMessageAsync(message.Id); } }
public override async Task PerformAction(SocketReaction option) { string number = option.Emote.ToString(); if (NUMBERS.Contains(number)) { int chosenIndex = Numbers.IndexOf(number); if (chosenIndex >= 0 && chosenIndex <= 10) { await selectedAction(RoleOptions[chosenIndex]); // Call the method passed in. } EventQueueManager.RemoveEvent(this); // Remove it from the queue if (Channel == null) { return; } await Channel.DeleteMessageAsync(Message); } }
private async Task DisplayAdventure(Adventure adventure) { try { // Get the channel for this adventure.. IMessageChannel channel = DiscordSocketClient.GetChannel(adventure.Channel) as IMessageChannel; // Get all players associated with this adventure. IEnumerable <Player> players = await PlayersRepository.GetAdventuringPlayersAsEnumerable(adventure.Id); // Get the adventureMessage that we will update. IUserMessage adventureMessage; // If we were passed the "RegenerateMessages" flag set to true, or if there is no Adventure Message, then create a new adventureMessage. if (adventure.RegenerateMessage || adventure.AdventureMessage == 0) { adventureMessage = await channel.SendMessageAsync("<ADVENTURE MESSAGE>") as IUserMessage; } // If we were passed the "RegenerateMessages" flag set to false, then retrieve the existing adventureMessage. else { adventureMessage = await channel.GetMessageAsync(adventure.AdventureMessage) as IUserMessage; } // Alter the messages to now have new status updates. EmbedBuilder embedBuilder = new EmbedBuilder(); embedBuilder.Color = Color.Blue; TimeSpan timePassed = DateTime.UtcNow - adventure.StartTime; string adventureHeader = $"ADVENTURE - {timePassed.ToString(@"mm\:ss")}"; embedBuilder.Title = adventureHeader; List <string> frontlinePlayers = new List <string>(); List <string> midlinePlayers = new List <string>(); List <string> backlinePlayers = new List <string>(); foreach (Player player in players) { 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 += "]"; string display = $"<@{player.Id}> ({player.Class}) \n HP {player.Health}/{player.MaxHealth} {healthBar} \n MP {player.Mana}/{Player.MaxMana} {manaBar}"; switch (player.AdventureRank) { case Adventure.Rank.Frontline: frontlinePlayers.Add(display); break; case Adventure.Rank.Midline: midlinePlayers.Add(display); break; case Adventure.Rank.Backline: backlinePlayers.Add(display); break; default: throw new Exception("Invalid Rank!"); } } string frontlinePlayerField = string.Join("\n\n", frontlinePlayers.ToArray()); if (string.IsNullOrEmpty(frontlinePlayerField)) { frontlinePlayerField = "<No Players>"; } string midlinePlayerField = string.Join("\n\n", midlinePlayers.ToArray()); if (string.IsNullOrEmpty(midlinePlayerField)) { midlinePlayerField = "<No Players>"; } string backlinePlayerField = string.Join("\n\n", backlinePlayers.ToArray()); if (string.IsNullOrEmpty(backlinePlayerField)) { backlinePlayerField = "<No Players>"; } embedBuilder.AddField("Player Frontline", frontlinePlayerField); embedBuilder.AddField("Player Midline", midlinePlayerField); embedBuilder.AddField("Player Backline", backlinePlayerField); embedBuilder.AddField("\u200b", "\u200b"); string adventureLog = string.Join("\n", (adventure.Logs ?? new List <string>()).ToArray()); if (string.IsNullOrEmpty(adventureLog)) { adventureLog = "<No Logs>"; } embedBuilder.AddField("Adventure Log", adventureLog); await adventureMessage.ModifyAsync(m => { m.Content = ""; m.Embed = embedBuilder.Build(); }); // If we were passed the "RegenerateMessages" flag set to true, or if there was no AdventureMessage, then delete old adventureMessage if possible. // Then, toggle the "RegenerateMessages" flag to false, and set the new adventureMessage on the Adventure. if (adventure.RegenerateMessage || adventure.AdventureMessage == 0) { if (adventure.AdventureMessage != 0) { await channel.DeleteMessageAsync(await channel.GetMessageAsync(adventure.AdventureMessage)); } await AdventuresRepository.SetNewChannelsForMessages ( adventureId : adventure.Id, adventureMessage : adventureMessage.Id ); } } catch (Exception e) { Console.WriteLine(e); } }
public async Task Purge([Remainder] string purgecommands) { try { var guser = Context.User as IGuildUser; if (!guser.GuildPermissions.Has(GuildPermission.ManageMessages)) { await Context.User.SendMessageAsync($"You do not have permissions to purge messages in {Context.Guild.Name}. Contact an admin if you wish to have messages removed!"); return; } var guildId = (null == Context.Guild ? Context.Channel.Id : Context.Guild.Id); var channel = Context.Channel; string user, count, purgeChannel; purgeChannel = count = user = string.Empty; //Parse arg string var commands = purgecommands.Split(' '); if (commands.Count() < 2 || commands.Count() > 3) { await channel.SendMessageAsync("Usage: !purge user number [channel]"); return; } if (commands.Count() >= 2) { user = commands[0].ToLower(); count = commands[1].ToLower(); } if (commands.Count() == 3) { purgeChannel = commands[2].ToLower(); } //Get channel IMessageChannel channel2purge = channel; if (!string.IsNullOrEmpty(purgeChannel)) { var textchannels = Context.Guild.GetTextChannelsAsync().Result; channel2purge = textchannels.First(x => x.Name.ToLower().Contains(purgeChannel)); } //Get user IUser user2purge = null; var users = channel2purge.GetUsersAsync().FirstOrDefault().Result; user2purge = users.First(x => (0 == string.Compare(x.Username, user, true))); foreach (IUser u in users) { if (0 == string.Compare(u.Username.ToLower(), user)) { user2purge = u; break; } } //Get count int number2purge = Int32.Parse(count); //Get messages; var msgs = channel2purge.GetMessagesAsync(1000).Flatten(); var usermsgs = msgs.Where(x => x.Author == user2purge); var msgs2purge = usermsgs.Take(number2purge); var enumerator = msgs2purge.GetEnumerator(); while (await enumerator.MoveNext()) { await channel2purge.DeleteMessageAsync(enumerator.Current); } } catch { } }
public static Task DeleteMessageAsync(this IMessageChannel channel, IMessage message, CancellationToken cancellationToken) => channel.DeleteMessageAsync(message, new RequestOptions { CancelToken = cancellationToken });