Skip to content

Commit

Permalink
Fix crash when importing on connection error
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethorbit committed Jun 21, 2022
1 parent b959322 commit f3319ef
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 77 deletions.
133 changes: 70 additions & 63 deletions DiscordBot/Importing/ChatImporter.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,81 +22,88 @@ public ChatImporter(IChatImporterSettings settings)

public async Task ImportNextMessage()
{
var expChan = this.Settings.Source;

if (this.IsFinished || expChan.Messages.Count <= 0)
try
{
this.IsFinished = true;
var expChan = this.Settings.Source;

if (this.FinishImports != null)
this.FinishImports(this, this.Settings);
if (this.IsFinished || expChan.Messages.Count <= 0)
{
this.IsFinished = true;

return;
}
if (this.FinishImports != null)
this.FinishImports(this, this.Settings);

Message msg = expChan.Messages.Peek();
DateTime msgTime = msg.Timestamp.UtcDateTime;
var author = DiscordFactory.CreateEmbedAuthor(msg.Author.Name, msg.Author.AvatarUrl);
var footer = DiscordFactory.CreateEmbedFooter($@"(Archived from: {expChan.Guild.Name}, #{expChan.Channel.Name} on: {msgTime.ToString("d")} at {msgTime.ToString("t")} UTC)");
return;
}

if (msg.Content.Length > 0) // Regular message text
{
await this.Settings.Destination.SendMessageAsync(null, false, DiscordFactory.CreateEmbed(null, msg.Content, Color.Gold, null, null, footer, author));
}
Message msg = expChan.Messages.Peek();
DateTime msgTime = msg.Timestamp.UtcDateTime;
var author = DiscordFactory.CreateEmbedAuthor(msg.Author.Name, msg.Author.AvatarUrl);
var footer = DiscordFactory.CreateEmbedFooter($@"(Archived from: {expChan.Guild.Name}, #{expChan.Channel.Name}. Original date: {msgTime.ToString("d")} at {msgTime.ToString("t")} UTC)");

//// Commented out because bots cannot create embeds with video players and stuff, so there's no sense trying to recreate them
//if (msg.Embeds.Count > 0) // Each URL generates separate embeds, we recreate those here
//{
// await Task.Run(async () =>
// {
// foreach (Export.Structures.Embed embed in msg.Embeds)
// {
// var embedFields = new EmbedField[embed.Fields.Length];
if (msg.Content.Length > 0) // Regular message text
{
await this.Settings.Destination.SendMessageAsync(null, false, DiscordFactory.CreateEmbed(null, msg.Content, Color.Gold, null, null, footer, author));
}

// foreach (Field field in embed.Fields)
// {
// embedFields[embedFields.Length] = DiscordFactory.CreateEmbedField(field.Name, field.Value, field.IsInline);
// }
//// Commented out because bots cannot create embeds with video players and stuff, so there's no sense trying to recreate them
//if (msg.Embeds.Count > 0) // Each URL generates separate embeds, we recreate those here
//{
// await Task.Run(async () =>
// {
// foreach (Export.Structures.Embed embed in msg.Embeds)
// {
// var embedFields = new EmbedField[embed.Fields.Length];

// Color? embedColor = null;
// foreach (Field field in embed.Fields)
// {
// embedFields[embedFields.Length] = DiscordFactory.CreateEmbedField(field.Name, field.Value, field.IsInline);
// }

// try
// {
// embedColor = (Color)System.Drawing.ColorTranslator.FromHtml(embed.Color);
// }
// finally
// {
// await this.Settings.Destination.SendMessageAsync(
// null,
// false,
// DiscordFactory.CreateEmbed
// (
// embed.Title,
// embed.Description,
// embedColor.HasValue ? embedColor.Value : default,
// embedFields.Length > 0 ? embedFields : null,
// embed.Thumbnail.HasValue ? embed.Thumbnail.Value.Url : null,
// embed.Footer.HasValue ? (EmbedFooter?)DiscordFactory.CreateEmbedFooter(embed.Footer.Value.Text, embed.Footer.Value.IconUrl) : null,
// embed.Author.HasValue ? (EmbedAuthor?)DiscordFactory.CreateEmbedAuthor(embed.Author.Value.Name, embed.Author.Value.IconUrl, embed.Author.Value.Url) : null
// )
// );
// }
// }
// });
//}

if (msg.Attachments.Count > 0) // Each attachment uploaded from a device, though they will only be displayed in their URL form here
{
await Task.Run(async () =>
// Color? embedColor = null;

// try
// {
// embedColor = (Color)System.Drawing.ColorTranslator.FromHtml(embed.Color);
// }
// finally
// {
// await this.Settings.Destination.SendMessageAsync(
// null,
// false,
// DiscordFactory.CreateEmbed
// (
// embed.Title,
// embed.Description,
// embedColor.HasValue ? embedColor.Value : default,
// embedFields.Length > 0 ? embedFields : null,
// embed.Thumbnail.HasValue ? embed.Thumbnail.Value.Url : null,
// embed.Footer.HasValue ? (EmbedFooter?)DiscordFactory.CreateEmbedFooter(embed.Footer.Value.Text, embed.Footer.Value.IconUrl) : null,
// embed.Author.HasValue ? (EmbedAuthor?)DiscordFactory.CreateEmbedAuthor(embed.Author.Value.Name, embed.Author.Value.IconUrl, embed.Author.Value.Url) : null
// )
// );
// }
// }
// });
//}

if (msg.Attachments.Count > 0) // Each attachment uploaded from a device, though they will only be displayed in their URL form here
{
foreach (Export.Structures.Attachment att in msg.Attachments)
await Task.Run(async () =>
{
await this.Settings.Destination.SendMessageAsync(null, false, DiscordFactory.CreateEmbed(null, att.Url, Color.Gold, null, null, footer, author));
}
});
}
foreach (Export.Structures.Attachment att in msg.Attachments)
{
await this.Settings.Destination.SendMessageAsync(null, false, DiscordFactory.CreateEmbed(null, att.Url, Color.Gold, null, null, footer, author));
}
});
}

expChan.Messages.Dequeue();
expChan.Messages.Dequeue();
}
catch (Exception e)
{
Console.WriteLine($"Message failed to import. - ({e.Message})");
}
}
}
}
24 changes: 10 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
## DISCONTINUED
This will not work, see: https://github.com/Ethorbit/Discord-Chat-Importer/issues/2

# Discord-Chat-Importer (DISCONTINUED)
Discord-Chat-Importer allows you to import Discord channels and chats (exported with DiscordChatExporter) across Discord servers.
# Discord-Chat-Importer
Discord-Chat-Importer allows you to import Discord channels and chats (that were exported from [DiscordChatExporter](https://github.com/Tyrrrz/DiscordChatExporter)) across Discord servers.

# Steps
## Exporting a channel to .json
* Download DiscordChatExporter: https://github.com/Tyrrrz/DiscordChatExporter
* Use DiscordChatExporter to export a channel to .json:
* Use DiscordChatExporter to export a channel to .json Format:
<br></br> [![Export Tutorial](https://i.imgur.com/78Ejkhp.jpg)](https://m.youtube.com/watch?v=tt-TBOWLyJk)

## Creating the bot
* Go to: https://discord.com/developers/applications
* Click "New Application" on the top-right.
* Give it a name and press `Create`
* On the left panel, click `OAuth2`
* On the left panel, click `Bot`
* Click `Add Bot`
* Copy the Token, you'll need this for later steps. (If you don't see a token, click "Reset")
* On the left panel again, click 'URL Generator' under `OAuth2`
* Under Scopes, check `Bot`
* Under Bot Permissions, check `View Channels, Send Messages, Manage Messages, Embed Links, Attach Files, Read Message History`
* Under Bot Permissions, check `Read Messages/View Channels, Send Messages, Manage Messages, Embed Links, Attach Files, Read Message History`
* Copy the URL generated under Scopes and paste it into your browser
* Select the server you want the bot in
* Go back to the applicaiton page
* On the left panel, click `Bot`
* Click `Add Bot`
* Copy the Token, you'll need this for the next step.

**WARNING:** DO NOT try to use the bot in multiple servers at once as it was designed to only support 1. Create more bots if you have to.

## Starting the bot
* Download the executable from: https://github.com/Ethorbit/Discord-Chat-Importer/releases
* Open the Command Prompt or Terminal
* Run the executable with the argument as the token
* Windows example: `"C:\Users\Ethorbit\source\repos\Discord-Channel-Importer\bin\Debug\netcoreapp3.1\Discord-Channel-Importer.exe" "MQENWQKNQWKRNWQRQOWR.WQEOJQWEODN3AKDNAKNXeIA"`
* Windows example: `"C:\Users\Joe\Downloads\Discord-Channel-Importer.exe" "MQENWQKNQWKRNWQRQOWR.WQEOJQWEODN3AKDNAKNXeIA"`


## Importing the .json
Expand All @@ -42,6 +38,6 @@ Discord-Chat-Importer allows you to import Discord channels and chats (exported
<br></br> ![Import Preview](https://i.imgur.com/SZ1bOq9.png)

## Commands
You need the Manage Messages and Manage Channels permissions.
You need the Manage Messages and Manage Channels permissions to use the bot commands.
<br></br>Type `!importer help` for the command list.
* ![Command Preview](https://i.imgur.com/I684Agh.png)

0 comments on commit f3319ef

Please sign in to comment.