<commit_before>"use strict";

/**************************
 * Import important stuff *
 **************************/

const messageConverter = require("./messageConverter");
const MessageMap = require("../MessageMap");
const R = require("ramda");
const mime = require("mime/lite");
const Bridge = require("../bridgestuff/Bridge");
const Discord = require("discord.js");
const request = require("request");

/**
 * Creates a function which sends files from Telegram to discord
 *
 * @param {Telegraf} tgBot	The Telegram bot
 * @param {Discord.Client} dcBot	The Discord bot
 * @param {MessageMap} messageMap	Map between IDs of messages
 * @param {Settings} settings	The settings to use
 *
 * @returns {Function}	A function which can be used to send files from Telegram to Discord
 *
 * @private
 */
function makeFileSender(tgBot, dcBot, messageMap, settings) {
	/**
	 * Sends a file to Discord
	 *
	 * @param {String} arg.discordChannel Discord channel ID
	 * @param {Message} arg.message	The message the file comes from
	 * @param {String} arg.fileId	ID of the file to download from Telegram's servers
	 * @param {String} arg.fileName	Name of the file to send
	 * @param {String} [arg.caption]	Additional text to send with the file
	 * @param {Boolean} [arg.resolveExtension]	Set to true if the bot should try to find the file extension itself, in which case it will be appended to the file name. Defaults to false
	 */
	return async function (bridge, {message, fileId, fileName, caption = "", resolveExtension = false}) {
		// Make the text to send
		const messageObj = messageConverter(message, tgBot, settings, dcBot, bridge);
		const textToSend = bridge.telegram.sendUsernames
			? `**${messageObj.from}**:\n${caption}`
			: caption
		;

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

		// Start getting the file
		const [file, fileLink] = await Promise.all([
			tgBot.telegram.getFile(fileId),
			tgBot.telegram.getFileLink(fileId)
		]);
		const fileStream = request(fileLink);

		// Get the extension, if necessary
		const extension = resolveExtension
			? "." + file.file_path.split(".").pop()
			: ""
		;

		// Send it to Discord
		const dcMessage = await dcBot.channels.get(bridge.discord.channelId).send(
			textToSend,
			new Discord.Attachment(fileStream, fileName + extension)
		);

		messageMap.insert(MessageMap.TELEGRAM_TO_DISCORD, bridge, message.message_id, dcMessage.id);
	};
}

/**
 * Makes a name object (for lack of better term) of a user object. It contains the user's full name, and the username or the text `No username`
 *
 * @param {User} user	The user object to make the name object of
 *
 * @returns {Object}	The name object, with `name` and `username` as properties
 */
function makeNameObject(user) {
	// Make the user's full name
	const name = user.first_name
		+ (user.last_name !== undefined
			? " " + user.last_name
			: ""
		);

	// Make the user's username
	const username = user.username !== undefined
		? "@" + user.username
		: "No username";

	return {
		name,
		username
	};
}

/**
 * Curryed function creating handlers handling messages which should not be relayed, and passing through those which should
 *
 * @param {Logger} logger	The Logger instance to log messages to
 * @param {Telegraf} tgBot	The Telegram bot
 * @param {BridgeMap} bridgeMap	Map of the bridges to use
 * @param {Function} func	The message handler to wrap
 * @param {Context} ctx	The Telegram message triggering the wrapped function, wrapped in a context
 *
 * @private
 */
const createMessageHandler = R.curry((logger, tgBot, bridgeMap, func, ctx) => {
	// Channel posts are not on ctx.message, but are very similar. Use the proper one
	const message = R.cond([
		// XXX I tried both R.has and R.hasIn as conditions. Neither worked for some reason
		[ctx => !R.isNil(ctx.channelPost), R.prop("channelPost")],
		[ctx => !R.isNil(ctx.editedChannelPost), R.prop("editedChannelPost")],
		[ctx => !R.isNil(ctx.message), R.prop("message")],
		[ctx => !R.isNil(ctx.editedMessage), R.prop("editedMessage")],
	])(ctx);

	if (message.text === "/chatinfo") {<commit_msg>Got rid of unnecessary comma<commit_after>"use strict";

/**************************
 * Import important stuff *
 **************************/

const messageConverter = require("./messageConverter");
const MessageMap = require("../MessageMap");
const R = require("ramda");
const mime = require("mime/lite");
const Bridge = require("../bridgestuff/Bridge");
const Discord = require("discord.js");
const request = require("request");

/**
 * Creates a function which sends files from Telegram to discord
 *
 * @param {Telegraf} tgBot	The Telegram bot
 * @param {Discord.Client} dcBot	The Discord bot
 * @param {MessageMap} messageMap	Map between IDs of messages
 * @param {Settings} settings	The settings to use
 *
 * @returns {Function}	A function which can be used to send files from Telegram to Discord
 *
 * @private
 */
function makeFileSender(tgBot, dcBot, messageMap, settings) {
	/**
	 * Sends a file to Discord
	 *
	 * @param {String} arg.discordChannel Discord channel ID
	 * @param {Message} arg.message	The message the file comes from
	 * @param {String} arg.fileId	ID of the file to download from Telegram's servers
	 * @param {String} arg.fileName	Name of the file to send
	 * @param {String} [arg.caption]	Additional text to send with the file
	 * @param {Boolean} [arg.resolveExtension]	Set to true if the bot should try to find the file extension itself, in which case it will be appended to the file name. Defaults to false
	 */
	return async function (bridge, {message, fileId, fileName, caption = "", resolveExtension = false}) {
		// Make the text to send
		const messageObj = messageConverter(message, tgBot, settings, dcBot, bridge);
		const textToSend = bridge.telegram.sendUsernames
			? `**${messageObj.from}**:\n${caption}`
			: caption
		;

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

		// Start getting the file
		const [file, fileLink] = await Promise.all([
			tgBot.telegram.getFile(fileId),
			tgBot.telegram.getFileLink(fileId)
		]);
		const fileStream = request(fileLink);

		// Get the extension, if necessary
		const extension = resolveExtension
			? "." + file.file_path.split(".").pop()
			: ""
		;

		// Send it to Discord
		const dcMessage = await dcBot.channels.get(bridge.discord.channelId).send(
			textToSend,
			new Discord.Attachment(fileStream, fileName + extension)
		);

		messageMap.insert(MessageMap.TELEGRAM_TO_DISCORD, bridge, message.message_id, dcMessage.id);
	};
}

/**
 * Makes a name object (for lack of better term) of a user object. It contains the user's full name, and the username or the text `No username`
 *
 * @param {User} user	The user object to make the name object of
 *
 * @returns {Object}	The name object, with `name` and `username` as properties
 */
function makeNameObject(user) {
	// Make the user's full name
	const name = user.first_name
		+ (user.last_name !== undefined
			? " " + user.last_name
			: ""
		);

	// Make the user's username
	const username = user.username !== undefined
		? "@" + user.username
		: "No username";

	return {
		name,
		username
	};
}

/**
 * Curryed function creating handlers handling messages which should not be relayed, and passing through those which should
 *
 * @param {Logger} logger	The Logger instance to log messages to
 * @param {Telegraf} tgBot	The Telegram bot
 * @param {BridgeMap} bridgeMap	Map of the bridges to use
 * @param {Function} func	The message handler to wrap
 * @param {Context} ctx	The Telegram message triggering the wrapped function, wrapped in a context
 *
 * @private
 */
const createMessageHandler = R.curry((logger, tgBot, bridgeMap, func, ctx) => {
	// Channel posts are not on ctx.message, but are very similar. Use the proper one
	const message = R.cond([
		// XXX I tried both R.has and R.hasIn as conditions. Neither worked for some reason
		[ctx => !R.isNil(ctx.channelPost), R.prop("channelPost")],
		[ctx => !R.isNil(ctx.editedChannelPost), R.prop("editedChannelPost")],
		[ctx => !R.isNil(ctx.message), R.prop("message")],
		[ctx => !R.isNil(ctx.editedMessage), R.prop("editedMessage")]
	])(ctx);

	if (message.text === "/chatinfo") {
{'fraction_non_alphanumeric': 0.1310336421257923, 'fraction_numerical': 0.0, 'mean_word_length': 4.043023970497849, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3840944', 'n_tokens_mistral': 2606, 'n_tokens_neox': 2433, 'n_words': 882}