static async Task DeleteDmsMessages(DiscordDmChannel channel) { Console.WriteLine($"Deleting messages...", Color.Green); ulong?first = null; int cnt = 0; do { var messages = await channel.GetMessagesAsync(100, first); if (messages.Count == 0) { break; } var ownMessages = messages.Where(k => k.Author.Id == client.CurrentUser.Id); cnt += ownMessages.Count(); first = messages.Last().Id; foreach (var message in ownMessages) { await channel.DeleteMessageAsync(message); } }while (first.HasValue); Console.WriteLine($"Deleted {cnt} messages!", Color.Green); }
private async Task <List <DiscordMessage> > GetDmChannelMessagesAsync(DiscordDmChannel dmChannel) { var messages = new List <DiscordMessage>(); ulong lastMessageId = 0; while (true) { var messagesBlock = lastMessageId == 0 ? await dmChannel.GetMessagesAsync() : await dmChannel.GetMessagesAsync(before : lastMessageId); if (messagesBlock != null && messagesBlock.Count != 0) { messages.AddRange(messagesBlock.ToList().FindAll(x => x.Author.Username == client.CurrentUser.Username)); lastMessageId = messagesBlock[messagesBlock.Count - 1].Id; } if (messagesBlock.Count < 100) { break; } } return(messages); }
private async Task FindMessages(DiscordLink plugin) { _targetDisplays.Clear(); foreach (DiscordTarget target in GetDiscordTargets()) { IReadOnlyList <DiscordMessage> targetMessages = null; ChannelLink channelLink = target as ChannelLink; UserLink userLink = target as UserLink; if (channelLink == null && userLink == null) { continue; } TargetDisplayData data = new TargetDisplayData(target); _targetDisplays.Add(data); if (channelLink != null) { if (!channelLink.IsValid()) { continue; } // Get the channel and verify permissions DiscordGuild discordGuild = plugin.GuildByNameOrId(channelLink.DiscordGuild); if (discordGuild == null) { continue; } DiscordChannel discordChannel = discordGuild.ChannelByNameOrId(channelLink.DiscordChannel); if (discordChannel == null) { continue; } if (!DiscordUtil.ChannelHasPermission(discordChannel, Permissions.ReadMessageHistory)) { continue; } targetMessages = await DiscordUtil.GetMessagesAsync(discordChannel); } else if (userLink != null) { DiscordDmChannel dmChannel = await userLink.Member.CreateDmChannelAsync(); targetMessages = await dmChannel.GetMessagesAsync(); } if (targetMessages == null) { // There was an error or no messages exist - Clean up and return _targetDisplays.Clear(); return; } // Go through the messages and find any our tagged messages foreach (DiscordMessage message in targetMessages) { if (!message.Content.StartsWith(BaseTag)) { continue; } data.MessageIDs.Add(message.Id); } } _dirty = false; }
private async void Clear(object sender, RoutedEventArgs e) { try { if (IsDirect.IsChecked ?? false) { DiscordGuild server; ulong serverId = Convert.ToUInt64(GuildId.Text); if (MainWindow.userClient.Guilds.TryGetValue(serverId, out server)) { ulong channelIdVal = Convert.ToUInt64(ChannelId.Text); try { DiscordMember dmUser = await server.GetMemberAsync(channelIdVal); DiscordDmChannel dm = await dmUser.CreateDmChannelAsync(); if (dm != null) { int msgCount; ulong startClear; if (!int.TryParse(ClearCount.Text, out msgCount)) { MessageBox.Show("Неправильное кол-во ссобщений."); } if (!ulong.TryParse(ClearerMsgId.Text, out startClear)) { MessageBox.Show("Неправильный ID сообщения."); } try { toClear = (await dm.GetMessagesAsync(msgCount, startClear)).Where(ex => ex.Author == MainWindow.userClient.CurrentUser).ToList(); MessageBoxResult result = MessageBox.Show($"Вы действительно хотите удалить свои {toClear.Count} сообщений в DM с \"{dmUser.Username}\" начиная с сообщения \"{toClear.Last().Content}\" и заканчивая \"{toClear.First().Content}\"?", "Ожидание подтверждения", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No); if (result == MessageBoxResult.No) { MessageBox.Show("Удаление отменено."); return; } foreach (DiscordMessage msg in toClear) { try { await dm.DeleteMessageAsync(msg); Thread.Sleep(1300); } catch (Exception) { Thread.Sleep(600); } } } catch (Exception) { MessageBox.Show("Какая-то ошибка."); } } else { MessageBox.Show("Ошибка: неверный ID пользователя."); } } catch (Exception) { MessageBox.Show("Ошибка: неверный ID пользователя."); } } } else { DiscordGuild server; ulong serverId = Convert.ToUInt64(GuildId.Text); if (MainWindow.userClient.Guilds.TryGetValue(serverId, out server)) { ulong channelIdVal = Convert.ToUInt64(ChannelId.Text); try { DiscordChannel channel = server.GetChannel(channelIdVal); if (channel != null) { int msgCount; ulong startClear; if (!int.TryParse(ClearCount.Text, out msgCount)) { MessageBox.Show("Неправильное кол-во ссобщений."); } if (!ulong.TryParse(ClearerMsgId.Text, out startClear)) { MessageBox.Show("Неправильный ID сообщения."); } try { if ((OnlyMine.IsChecked ?? false) == false) { toClear = (await channel.GetMessagesAsync(msgCount, startClear)).ToList(); } else { toClear = (await channel.GetMessagesAsync(msgCount, startClear)).Where(ex => ex.Author == MainWindow.userClient.CurrentUser).ToList(); } MessageBoxResult result = MessageBox.Show($"Вы действительно хотите удалить {toClear.Count}{((OnlyMine.IsChecked ?? false)?" своих":"")} сообщений в канале \"{channel.Name}\" на сервере \"{server.Name}\" начиная с сообщения \"{toClear.Last().Content}\" и заканчивая \"{toClear.First().Content}\"?", "Ожидание подтверждения", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No); if (result == MessageBoxResult.No) { MessageBox.Show("Удаление отменено."); return; } foreach (DiscordMessage msg in toClear) { try { await channel.DeleteMessageAsync(msg); Thread.Sleep(1300); } catch (Exception) { Thread.Sleep(600); } } } catch (Exception) { MessageBox.Show("Какая-то ошибка."); } } else { MessageBox.Show("Ошибка: неверный ID канала."); } } catch (Exception) { MessageBox.Show("Ошибка: неверный ID канала."); } } else { MessageBox.Show("Ошибка: неверный ID сервера."); } } } catch (Exception) { return; } }