Skip to content

Commit

Permalink
+ 0. Updated to work with latest packages
Browse files Browse the repository at this point in the history
+ 2. Option to disable "too big file" message (in telegram)
+ 2.1. If one of files in a message is too big - the rest still being rerouted
+ 2.2. Fixed names of files sent from telegram
+ 5. User first & last name form Telegram used when no username set
  • Loading branch information
kutuk committed Jul 20, 2023
1 parent f2e8d1a commit 259cb18
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 71 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ data/

# Built files
dist/
.vscode/settings.json
.vscode/settings.json
/todo.txt
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tedicross",
"version": "0.11.4",
"version": "0.11.6",
"description": "Bridging Telegram and Discord",
"license": "MIT",
"repository": {
Expand All @@ -18,30 +18,30 @@
"dependencies": {
"@types/js-yaml": "^4.0.5",
"@types/mime": "^3.0.1",
"@types/ramda": "^0.29.2",
"@types/ramda": "^0.29.3",
"@types/semver": "^7.5.0",
"@types/yargs": "^17.0.24",
"@typescript-eslint/eslint-plugin": "^5.59.7",
"@typescript-eslint/parser": "^5.59.7",
"@typescript-eslint/eslint-plugin": "^6.1.0",
"@typescript-eslint/parser": "^6.1.0",
"discord.js": "^14.11.0",
"js-yaml": "^4.1.0",
"mime": "^3.0.0",
"moment": "^2.29.4",
"ramda": "^0.29.0",
"semver": "^7.5.1",
"semver": "^7.5.4",
"simple-markdown": "^0.7.3",
"sqlite": "^4.2.1",
"sqlite": "^5.0.1",
"sqlite3": "^5.1.6",
"telegraf": "^4.12.2",
"typescript": "^5.0.4",
"typescript": "^5.1.6",
"yargs": "^17.7.2"
},
"devDependencies": {
"eslint": "^8.41.0",
"eslint": "^8.45.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-prettier": "^4.2.1",
"nodemon": "^2.0.22",
"prettier": "^2.8.8",
"eslint-plugin-prettier": "^5.0.0",
"nodemon": "^3.0.1",
"prettier": "^3.0.0",
"rimraf": "^5.0.1",
"ts-node": "^10.9.1"
}
Expand Down
4 changes: 2 additions & 2 deletions src/discord2telegram/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export function setup(
.reply("\nchannelId: '" + message.channel.id + "'")
.then(sleepOneMinute)
.then((info: any) => Promise.all([info.delete(), message.delete()]))
.catch(ignoreAlreadyDeletedError);
.catch(ignoreAlreadyDeletedError as any);

// Don't process the message any further
return;
Expand Down Expand Up @@ -300,7 +300,7 @@ export function setup(
// Delete it again after some time
.then(sleepOneMinute)
.then((message: any) => message.delete())
.catch(ignoreAlreadyDeletedError)
.catch(ignoreAlreadyDeletedError as any)
.then(() => antiInfoSpamSet.delete(message.channel.id));
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/fetchDiscordChannel.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Client, TextChannel } from "discord.js";
import R from "ramda";

/**
* Gets a Discord channel, and logs an error if it doesn't exist
*
* @returns A Promise resolving to the channel, or rejecting if it could not be fetched for some reason
*/
export const fetchDiscordChannel = R.curry((dcBot: Client, bridge) => {
// export const fetchDiscordChannel = R.curry((dcBot: Client, bridge) => {
export const fetchDiscordChannel = (dcBot: Client, bridge: any) => {
// Get the channel's ID
const channelId = bridge.discord.channelId;

Expand All @@ -15,5 +15,4 @@ export const fetchDiscordChannel = R.curry((dcBot: Client, bridge) => {
console.error(`Could not find Discord channel ${channelId} in bridge ${bridge.name}: ${err.message}`);
throw err;
}) as unknown as Promise<TextChannel>;
});

};
32 changes: 22 additions & 10 deletions src/settings/DiscordSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ interface Settings {
useNickname: boolean;
maxReplyLines: number;
skipOldMessages: boolean;
suppressFileTooBigMessages: boolean;
}

/*****************************
Expand All @@ -21,6 +22,7 @@ export class DiscordSettings {
replyLength: number;
maxReplyLines: number;
skipOldMessages: boolean;
suppressFileTooBigMessages: boolean;

/**
* Creates a new DiscordSettings object
Expand Down Expand Up @@ -49,6 +51,9 @@ export class DiscordSettings {

/** How many lines of the original message to show in replies from Telegram */
this.maxReplyLines = settings.maxReplyLines;

/** Don't send a warning message to Discord if a file is too big to be sent from Telegram to Discord */
this.suppressFileTooBigMessages = settings.suppressFileTooBigMessages;
}

/** The bot token to use */
Expand Down Expand Up @@ -79,15 +84,16 @@ export class DiscordSettings {
* @throws If the object is not suitable. The error message says what the problem is
*/
static validate(settings: Settings) {
// Check that the settings are indeed in object form
if (!(settings instanceof Object)) {
throw new Error("`settings` must be an object");
}

// Check that the token is a string
if (typeof settings.token !== "string") {
throw new Error("`settings.token` must be a string");
}
// NOTE: redundant checks
// // Check that the settings are indeed in object form
// if (!(settings instanceof Object)) {
// throw new Error("`settings` must be an object");
// }
//
// // Check that the token is a string
// if (typeof settings.token !== "string") {
// throw new Error("`settings.token` must be a string");
// }

// Check that skipOldMessages is a boolean
if (Boolean(settings.skipOldMessages) !== settings.skipOldMessages) {
Expand All @@ -108,6 +114,11 @@ export class DiscordSettings {
if (!Number.isInteger(settings.maxReplyLines) || settings.maxReplyLines <= 0) {
throw new Error("`settings.maxReplyLines` must be an integer greater than 0");
}

// Check that `suppressFileTooBigMessages` is a boolean
if (Boolean(settings.suppressFileTooBigMessages) !== settings.suppressFileTooBigMessages) {
throw new Error("`settings.suppressFileTooBigMessages` must be a boolean");
}
}

/** Constant telling the Discord token should be gotten from the environment */
Expand All @@ -120,7 +131,8 @@ export class DiscordSettings {
return {
token: DiscordSettings.GET_TOKEN_FROM_ENVIRONMENT,
skipOldMessages: true,
useNickname: false
useNickname: false,
suppressFileTooBigMessages: false
};
}
}
17 changes: 12 additions & 5 deletions src/telegram2discord/From.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,16 @@ export function createFromObjFromChat(chat: Record<string, any>) {
* @returns The display name
*/
export function makeDisplayName(useFirstNameInsteadOfUsername: boolean, from: From) {
return R.ifElse<any, any, any>(
from => useFirstNameInsteadOfUsername || R.isNil(from.username),
R.prop("firstName"),
R.prop("username")
)(from);
if (useFirstNameInsteadOfUsername || !from.username) {
const prefix: string = from.lastName ? ` ${from.lastName}` : "";
return `${from.firstName}${prefix}`;
}

return from.username;
// return R.ifElse<any, any, any>(
// from => R.isNil(from.username) || useFirstNameInsteadOfUsername,
// // (useFirstNameInsteadOfUsername || R.isNil(from.username)),
// from => R.prop("firstName", from),
// from => R.prop("username", from)
// )(from);
}
13 changes: 7 additions & 6 deletions src/telegram2discord/endwares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const chatinfo = (ctx: TediCrossContext, next: () => void) => {
ctx.deleteMessage()
])
)
.catch(ignoreAlreadyDeletedError);
.catch(ignoreAlreadyDeletedError as any);
} else {
next();
}
Expand All @@ -98,7 +98,7 @@ export const newChatMembers = createMessageHandler((ctx: TediCrossContext, bridg

// Pass it on
ctx.TediCross.dcBot.ready
.then(() => fetchDiscordChannel(ctx.TediCross.dcBot, bridge).then(channel => channel.send(text)))
.then(() => fetchDiscordChannel(ctx.TediCross.dcBot, bridge).then((channel: any) => channel.send(text)))
.catch((err: any) =>
console.error(`Could not tell Discord about a new chat member on bridge ${bridge.name}: ${err.message}`)
);
Expand Down Expand Up @@ -139,7 +139,7 @@ export const leftChatMember = createMessageHandler((ctx: TediCrossContext, bridg
* @param ctx.tediCross The TediCross context of the message
* @param ctx.TediCross The global TediCross context of the message
*/
export const relayMessage = (ctx: TediCrossContext) =>
export const relayMessage = (ctx: TediCrossContext) => {
R.forEach(async (prepared: any) => {
try {
// Wait for the Discord bot to become ready
Expand Down Expand Up @@ -172,6 +172,7 @@ export const relayMessage = (ctx: TediCrossContext) =>
}
chunks = R.tail(chunks);
} catch (err: any) {
console.dir(`relayMessage err: ${err.message}`);
if (err.message === "Request entity too large") {
dcMessage = await channel.send(
`***${prepared.senderName}** on Telegram sent a file, but it was too large for Discord. If you want it, ask them to send it some other way*`
Expand Down Expand Up @@ -206,6 +207,7 @@ export const relayMessage = (ctx: TediCrossContext) =>
console.error(`Could not relay a message to Discord on bridge ${prepared.bridge.name}: ${err.message}`);
}
})(ctx.tediCross.prepared);
};

/**
* Handles message edits
Expand All @@ -216,12 +218,11 @@ export const handleEdits = createMessageHandler(async (ctx: TediCrossContext, br
// Function to "delete" a message on Discord
const del = async (ctx: TediCrossContext, bridge: any) => {
try {

// Wait for the Discord bot to become ready
await ctx.TediCross.dcBot.ready;

// Find the ID of this message on Discord
let [dcMessageId] = await ctx.TediCross.messageMap.getCorresponding(
const [dcMessageId] = await ctx.TediCross.messageMap.getCorresponding(
MessageMap.TELEGRAM_TO_DISCORD,
bridge,
ctx.tediCross.message.message_id
Expand Down Expand Up @@ -254,7 +255,7 @@ export const handleEdits = createMessageHandler(async (ctx: TediCrossContext, br
await ctx.TediCross.dcBot.ready;

// Find the ID of this message on Discord
let [dcMessageId] = await ctx.TediCross.messageMap.getCorresponding(
const [dcMessageId] = await ctx.TediCross.messageMap.getCorresponding(
MessageMap.TELEGRAM_TO_DISCORD,
bridge,
tgMessage.message_id
Expand Down
Loading

0 comments on commit 259cb18

Please sign in to comment.