/// <summary> /// Clean up a channel /// </summary> /// <param name="channel">The channel to clean up</param> private async static void Clean(IMessageChannel channel) { // Remove all announcements that have expired from both the channel and the config (where necessary) var announcements = Config.Instance.ChannelConfigs.Where(x => x.ChannelId == channel.Id).Single().ChannelData.Announcements; List <ulong> toRemove = new List <ulong>(); foreach (var a in announcements) { var message = await channel.GetMessageAsync(a.Key); if (message == null) { toRemove.Add(a.Key); } else { if (a.Value < DateTime.Now) { List <IMessage> toDelete = new List <IMessage>(); toDelete.Add(message); await channel.DeleteMessagesAsync(toDelete); toRemove.Add(a.Key); } } } foreach (var remove in toRemove) { announcements.Remove(remove); } Config.Instance.Write(); }
/** * ClearMessages * Clears [num] number of messages from the current text channel. * * @param guild * @param channel * @param num */ 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)E_LogOutput.Reply); 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)E_LogOutput.Reply); return; } // Delete. var messages = await channel.GetMessagesAsync((int)num + 1).Flatten(); await channel.DeleteMessagesAsync(messages); // Reply with status. Log($"{user.Username} deleted {num} messages", (int)E_LogOutput.Reply); }
public static async Task DeleteMessage(ulong guildId, ulong channelId, ulong messageId) { IGuild guild = null; IMessageChannel channel = null; if (guildId != 0 && channelId != 0) { guild = Program.client.GetGuild(guildId); if (guild != null) { try { var messages = new List <IMessage>(); channel = (IMessageChannel)await guild.GetChannelAsync(channelId); messages.Add(await channel.GetMessageAsync(messageId)); await channel.DeleteMessagesAsync(messages); } catch (Exception ex) { channel = null; } } } }
public async Task DeleteMessages(IGuildUser user, IMessageChannel channel = null) { System.Collections.Generic.IEnumerable <IMessage> msgs = null; List <IMessage> deleteMsgs = new List <IMessage>(); if (channel == null) { channel = Context.Channel; msgs = await Context.Channel.GetMessagesAsync().Flatten(); } else { msgs = await channel.GetMessagesAsync().Flatten(); } foreach (var msg in msgs) { if (msg.Author == user) { deleteMsgs.Add(msg); } } await channel.DeleteMessagesAsync(deleteMsgs); await new ChangeLog().LogChange($"[{Context.Message.Timestamp}]: {Context.Message.Author.Mention} deleted messages from {user.Nickname} in {MentionUtils.MentionChannel(channel.Id)}.", Context); }
public async Task Clear(ulong id) { await Context.Message.DeleteAsync(); IMessageChannel channel = Context.Channel; List <IMessage> messages = (await channel.GetMessagesAsync(id, Direction.After).Flatten()).ToList(); await channel.DeleteMessagesAsync(messages); await ReplyAsync($"{messages.Count} messages deleted."); }
private static async Task TryDeleteBatchAsync(IMessageChannel channel, IGuildUser guildUser, IEnumerable <IMessage> messages) { try { await channel.DeleteMessagesAsync(messages); } catch (Exception e) { LogManager.GetLogger("FAQ").Warn(e, $"Unable to delete messages {string.Join(", ", messages.Select(message => message.Id))} from {guildUser} in {guildUser.Guild}/{channel}"); } }
public async Task Clear(int amount) { if (amount < 1) { await ReplyAsync("cant delete less than one message").DeleteAfterSeconds(15); return; } await Context.Message.DeleteAsync(); IMessageChannel channel = Context.Channel; List <IMessage> messages = (await channel.GetMessagesAsync(amount).Flatten()).ToList(); messages.RemoveAll(x => x.Timestamp.Day > 14); await channel.DeleteMessagesAsync(messages); await ReplyAsync($"{messages.Count} messages deleted."); }
protected async Task DeleteAllMessages(IMessageChannel channel) { var messages = channel.GetMessagesAsync().Flatten().Result; await channel.DeleteMessagesAsync(messages); }