Skip to content

Commit

Permalink
csv export for messages
Browse files Browse the repository at this point in the history
  • Loading branch information
PizzaConsole committed Jun 6, 2023
1 parent d8e8fb9 commit 29972c6
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 133 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: [Poisnfang]
7 changes: 1 addition & 6 deletions Commands/BasicCommandsModule.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using DSharpPlus.Interactivity;
using DSharpPlus.Interactivity.Extensions;
using System;
using System.Threading.Tasks;

namespace PoisnCopy.Commands;

Expand Down
96 changes: 89 additions & 7 deletions Commands/CopyChannelCommand.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using DSharpPlus;
using CsvHelper;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using DSharpPlus.Interactivity;
using DSharpPlus.Interactivity.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Globalization;
using System.IO;
using static DSharpPlus.Entities.DiscordEmbedBuilder;

namespace PoisnCopy.Commands;
Expand Down Expand Up @@ -78,6 +76,9 @@ public async Task CopyChannel(CommandContext ctx)
await ctx.RespondAsync(
$"Copy command: `pc.loadchannel {selectedChannel.Value.GuildId} {selectedChannel.Value.Id}`"
);
await ctx.RespondAsync(
$"Export command: `pc.exportchannel {selectedChannel.Value.GuildId} {selectedChannel.Value.Id}`"
);
}

[Command("loadchannel")]
Expand Down Expand Up @@ -182,4 +183,85 @@ await ctx.Channel.SendMessageAsync(

await ctx.RespondAsync($"{newChan.Name} copy complete!");
}
}

[Command("exportchannel")]
[Description("Export a copied channel")]
public async Task ExportChannel(CommandContext ctx, ulong guildId, ulong channelId)
{
var guild = await ctx.Client.GetGuildAsync(guildId);
var selectedChannel = guild.GetChannel(channelId);

await ctx.Channel.SendMessageAsync("Starting export...");

await ctx.Channel.SendMessageAsync("Collecting messages...");

var messag = await selectedChannel.GetMessagesAsync();

var messCopy = messag.ToList();
var more = await selectedChannel.GetMessagesAsync(100);

while (more.Count > 0)
{
messCopy.AddRange(more);
more = await selectedChannel.GetMessagesBeforeAsync(more.LastOrDefault().Id, 100);
}

await ctx.Channel.SendMessageAsync("Organizing messages...");

messCopy.Reverse();

var messageExports = new List<MessageExport>();

await ctx.Channel.SendMessageAsync(
$"Exporting {messCopy.Count} messages... (this could take awhile)"
);

foreach (var mes in messCopy)
{
if (!string.IsNullOrEmpty(mes.Content))
{
var textMessage = new MessageExport
{
AuthorName = mes.Author.Username,
IconUrl = mes.Author.AvatarUrl,
MessageConent = mes.Content,
Timestamp = mes.Timestamp.ToString("o")
};
messageExports.Add(textMessage);
}

if (mes.Attachments.Count > 0)
{
foreach (var att in mes.Attachments)
{
var imageMessage = new MessageExport
{
AuthorName = mes.Author.Username,
IconUrl = mes.Author.AvatarUrl,
MessageConent = att.Url,
Timestamp = mes.Timestamp.ToString("o")
};
messageExports.Add(imageMessage);
}
}
}

try
{
using var memStream = new MemoryStream();
using var writer = new StreamWriter(memStream);
using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
csv.WriteRecords(messageExports);
await writer.FlushAsync();
memStream.Position = 0;
var fileMessage = new DiscordMessageBuilder() { Content = "Messages exported" };
fileMessage.AddFile($"{selectedChannel.Name}-export.csv", memStream, true);

await ctx.Channel.SendMessageAsync(fileMessage);
}
catch (Exception e)
{
await ctx.Channel.SendMessageAsync(e.Message);
}
}
}
Binary file removed Images/snapshot-bmc-button.png
Binary file not shown.
9 changes: 9 additions & 0 deletions MessageExport.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace PoisnCopy;

internal class MessageExport
{
public string AuthorName { get; set; }
public string IconUrl { get; set; }
public string MessageConent { get; set; }
public string Timestamp { get; set; }
}
1 change: 1 addition & 0 deletions PoisnCopy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CsvHelper" Version="30.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
Expand Down
Loading

0 comments on commit 29972c6

Please sign in to comment.