Skip to content

Instantly share code, notes, and snippets.

@SuppliedOrange
Last active March 10, 2024 10:51
Show Gist options
  • Save SuppliedOrange/8194b28ccc758d7e55494e284c88b497 to your computer and use it in GitHub Desktop.
Save SuppliedOrange/8194b28ccc758d7e55494e284c88b497 to your computer and use it in GitHub Desktop.
This is probably the longest Discord.js command I ever made. Criticism is welcome
export const translate: MachinaFunction = machinaDecoratorInfo
({monikers: ["translate"], description: "translates."})
("webhook-commands", "translate", async (params: MachinaFunctionParameters) => {
if (!params.msg.member.hasPermission("MANAGE_CHANNELS")) return params.msg.channel.send({embed: { title: "You need the MANAGE_CHANNELS permission to use this, " + params.msg.author.tag, color: "4B0082" }});
if (params.args[0] == "help") {
let transhelp = new MessageEmbed({
title: "Translation Commands",
fields: [
{name: "The to-from channel", value: "You will have to be in the channel that you want the messages to be broadcasted from, and then choose the channel you would like to broadcast to"},
{name: "The 'translate from' language", value: "You will want to set a language ISO code for the messages to be translated from the current channel, into the translate-to language in the target channel"},
{name: "The 'translate-to' value", value: "You will want to set a language ISO code for the messages to be translated from the broadcasting channel, into the this language you want it to be translated to"},
{name: "Resetting the data", value: "**wb translate reset** will remove all configurations and disable the bot's ability to translate further"},
{name: "Disabling any value", value: "sending 'disable' while setting up the language will remove that value"},
{name: "Finding this confusing", value: "Of course this is confusing, a large chunk of meaningless text to most. Talk to me and i'll walk you through it personally. Also to discuss special features for your server like setting up more translation channels <@735322421862727760>"},
],
color: arrays.colors[Math.floor(Math.random() * arrays.colors.length)]
});
return params.msg.channel.send(transhelp);
}
let translate = await Translate.findOne({ serverid: params.msg.guild.id });
if(translate == null) {
let m = await params.msg.channel.send("```Performing first time setup for translation```");
const doc = new Translate({ serverid: params.msg.guild.id });
await doc.save();
m.delete();
translate = await Translate.findOne({ serverid: params.msg.guild.id })
}
async function retreat () {
await Translate.findOneAndUpdate({ serverid: params.msg.guild.id }, { $set: { tochannel: "none" } }, { new: true });
await Translate.findOneAndUpdate({ serverid: params.msg.guild.id }, { $set: { fromchannel: "none" } }, { new: true });
await Translate.findOneAndUpdate({ serverid: params.msg.guild.id }, { $set: { translateto: "none" } }, { new: true });
await Translate.findOneAndUpdate({ serverid: params.msg.guild.id }, { $set: { translatefrom: "none" } }, { new: true });
return params.msg.channel.send({embed: { title: "Your data was reset, lets get back to building those translation channels! Please note that neither of your translation channels will work now." , color: "4B0082" }});
}
if (params.args[0] == "reset") return retreat();
if (!translate) {
const doc = new Translate({ serverid: params.msg.guild.id });
await doc.save();
let translate = await Translate.findOne({serverid: params.msg.guild.id});
}
let process: string;
let pending = []
let incomplete = [" Translation-from channel "," Translation-to channel "," Translate-from Language ", " Translation-to Language "]
if (translate.tochannel == "none" && translate.fromchannel == "none") pending.push("fromchannel","tochannel"); else incomplete.splice(0,2)
if (translate.translatefrom == "none") pending.push("translatefrom"); else incomplete.splice(incomplete.indexOf(" Translation Language from "), 1);
if (translate.to == "none") pending.push("translateto"); else incomplete.splice(incomplete.indexOf(" Translated Language to "), 1);
function determineprocess(process) {
if (process == "settofrom") {
if (!pending.includes("fromchannel") && !pending.includes("tochannel")) params.msg.channel.send({embed: { title: "You will be modifying the values you had already set previously", description: `Current Sequence: \n Channel <-------> Translator\n <#${translate.fromchannel}> <------> <#${translate.tochannel}>` , color: "4B0082" }});
params.msg.channel.send({embed: { title: "The current channel chosen will be considered as the channel being translated from" , color: "4B0082" }});
params.msg.channel.send({embed: { title: "Mention the channel you would like to translate to from this channel \n Say 'cancel' to cancel \n Say 'disable' to reset these values!" , color: "4B0082" }});
params.msg.channel.awaitMessages(m => m.author == params.msg.author && (m.mentions.channels.first() || m.content.toLowerCase() == "cancel" || m.content.toLowerCase() == "disable"), { max: 1, time: 24000, errors: ['time'] })
.then(async collected => {
if (collected.first().content.toLowerCase() == "cancel") return params.msg.channel.send("Cancelled!");
if (collected.first().content.toLowerCase() == "disable") {
await Translate.findOneAndUpdate({ serverid: params.msg.guild.id }, { $set: { tochannel: "none" } }, { new: true });
await Translate.findOneAndUpdate({ serverid: params.msg.guild.id }, { $set: { fromchannel: "none" } }, { new: true });
return params.msg.channel.send("Reset channels, you will need to set them again to work");
}
let seq1; let seq2;
if(collected.first().mentions.channels.first()) {
let chosen = collected.first().mentions.channels.first().id
if (chosen == params.msg.channel.id) return params.msg.channel.send({embed: { title: "Your translation channels cannot be the same!" , color: "4B0082" }})
await Translate.findOneAndUpdate({ serverid: params.msg.guild.id }, { $set: { tochannel: chosen } }, { new: true });
await Translate.findOneAndUpdate({ serverid: params.msg.guild.id }, { $set: { fromchannel: params.msg.channel.id } }, { new: true });
seq1 = chosen
seq2 = params.msg.channel.id
}
else {
params.msg.channel.send({embed: { title: "Please retry", description: "Mention the channel you would like for the translation channel to be set to \n 'cancel' to cancel" , color: "4B0082" }})
return determineprocess("settofrom")
}
params.msg.channel.send({embed: { title: "Done!", description:`Sequence: \n Channel <-------> Translator\n <#${seq2}> <------> <#${seq1}>`, color: "4B0082" }});
}).catch(e => {
params.msg.channel.send({embed: { title: "No message recieved from " + params.msg.author.username, description: "Redo the command to retry" , color: "4B0082" }});
console.log(e.stack)
});
}
if (process == "translateto") {
if (!pending.includes("translateto")) params.msg.channel.send({embed: { title: "You will be modifying the values you had already set previously", description: "Current language: " + translate.translateto , color: "4B0082" }});
params.msg.channel.send({embed: { title: "Enter the ISO-639-1 code of the Language you would like for it to be translated to", description: "Example: English -> EN \nFrench -> FR\nJapanese -> JA\n If you don't know the ISO code of your language, please check https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes\n 'cancel' to cancel\n'disable' to disable this language", color: "4B0082" }})
params.msg.channel.awaitMessages(m => m.author == params.msg.author, { max: 1, time: 30000, errors: ['time'] })
.then(async collected => {
let lang: string
lang = collected.first().content;
if (lang.toLowerCase() == "disable") {
await Translate.findOneAndUpdate({ serverid: params.msg.guild.id }, { $set: { translateto: "none" } }, { new: true });
return params.msg.channel.send("Disabled this language value!")
}
if (lang.toLowerCase() == "cancel") return params.msg.channel.send("Cancelled!");
if (lang.length != 2) {
params.msg.channel.send({embed: { title: "Uh oh, are you sure that's an ISO-639-1 code?" , description: "Example: English -> EN \nFrench -> FR\nJapanese -> JA\nIf you don't know the ISO code of your language, please check https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes" , color: "4B0082" }})
return determineprocess("translateto");
}
await Translate.findOneAndUpdate({ serverid: params.msg.guild.id }, { $set: { translateto: lang.toLowerCase() } }, { new: true });
if (translate.translateto != "none") translatetolang = languages.getName(translate.translateto, "en");
let gremlin = (languages.getName(lang,"en") != undefined) ? "Our language determining gremlins have detected that your language is likely " + languages.getName(lang,"en") : "Our language detecting gremlins were unable to find your language (Probably because it is an invalid code!)"
params.msg.channel.send({embed: { title: "Updated your ISO code to " + lang.toLowerCase() + "! \nIf this isn't a valid code, translation may not work!\n\n" + gremlin , color: "4B0082" }})
}).catch(e => {
params.msg.channel.send({embed: { title: "No message recieved from " + params.msg.author.username, description: "Redo the command to retry" , color: "4B0082" }});
console.log(e.stack)
});
}
if (process == "translatefrom") {
if (!pending.includes("translatefrom")) params.msg.channel.send({embed: { title: "You will be modifying the values you had already set previously", description: "Current language: " + translate.translatefrom , color: "4B0082" }});
params.msg.channel.send({embed: { title: "Enter the ISO-639-1 code of the Language you would like for it to be translated from", description: "Example: English -> EN \nFrench -> FR\nJapanese -> JA\n For Auto-detect, simply use `auto` as the code (Note that this only translates to english)\n If you don't know the ISO code of your language, please check https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes\n 'cancel' to cancel\n'disable' to disable this language", color: "4B0082" }})
params.msg.channel.awaitMessages(m => m.author == params.msg.author, { max: 1, time: 30000, errors: ['time'] })
.then(async collected => {
let lang: string
lang = collected.first().content;
if (lang.toLowerCase() == "cancel") return params.msg.channel.send("Cancelled!");
if (lang.toLowerCase() == "disable") {
await Translate.findOneAndUpdate({ serverid: params.msg.guild.id }, { $set: { translatefrom: "none" } }, { new: true });
return params.msg.channel.send("Disabled this language value!")
}
if (lang.length != 2 && lang.toLowerCase() != "auto") {
params.msg.channel.send({embed: { title: "Uh oh, are you sure that's an ISO-639-1 code? Please retry or cancel" , description: "Example: English -> EN \nFrench -> FR\nJapanese -> JA\nIf you don't know the ISO code of your language, please check https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes" , color: "4B0082" }})
return determineprocess("translatefrom");
}
await Translate.findOneAndUpdate({ serverid: params.msg.guild.id }, { $set: { translatefrom: lang.toLowerCase() } }, { new: true });
let gremlin = (languages.getName(lang,"en") != undefined) ? "Our language determining gremlins have detected that your language is likely " + languages.getName(lang,"en") : "Our language detecting gremlins were unable to find your language (Probably because its set to auto or is an invalid code!)"
params.msg.channel.send({embed: { title: "Updated your ISO code to " + lang.toLowerCase() + "! \nIf this isn't a valid code, translation may not work!\n\n" + gremlin, color: "4B0082" }})
}).catch(e => {
params.msg.channel.send({embed: { title: "No message recieved from " + params.msg.author.username, description: "Redo the command to retry" , color: "4B0082" }});
console.log(e.stack)
});
}
}
let translatetolang: string, translatefromlang: string;
if (!["none"].includes(translate.translateto)) translatetolang = languages.getName(translate.translateto, "en");
if (!["none","auto"].includes(translate.translatefrom)) translatefromlang = languages.getName(translate.translatefrom, "en");
let fromchannelstatus = (translate.fromchannel == "none") ? "Not set up!" : `Translation from channel: <#${translate.fromchannel}>`;
let tochannelstatus = (translate.tochannel == "none") ? "Not set up!" : `Translation to channel: <#${translate.tochannel}>`;
let transfromstatus = (translate.translatefrom == "none") ? "Not set up!" : "Translating from " + (
(translate.translatefrom == "auto") ? "Auto-translate" : translatefromlang
) + "!";
let transtostatus = (translate.translateto == "none") ? "Not set up!" : "Translating to " + translatetolang + "!";
let pendingreminder = (!incomplete.length) ? "Your channels should be operational, provided that access to channels exists." : "You are yet to set the following values for an operational translation channel: ";
let incompleteinspector = incomplete;
if (incomplete.length < 1) incompleteinspector = ["All set! You're good to go"];
let translatedata = new MessageEmbed({
title: "Channel translation for " + params.msg.guild.name,
description: "For detailed help, use **wb translate help** \n Pick a reaction to change the values!",
fields: [
{name: "📥 Channel being translated to", value: tochannelstatus},
{name: "📤 Channel being translated from ", value: fromchannelstatus},
{name: "🔴 Language being translated from ", value: transfromstatus},
{name: "🔵 Language translating to ", value: transtostatus},
{name: pendingreminder, value: incompleteinspector.join(',')}
],
color: '#'+Math.floor(Math.random()*16777215).toString(16),
});
let translateembed = await params.msg.channel.send(translatedata);
const fromchnemoji = '📤';
const transfromemoji = '🔴';
const transtoemoji = '🔵';
translateembed.react(fromchnemoji).then(() => translateembed.react(transfromemoji)).then(() => translateembed.react(transtoemoji)).catch(e => params.msg.channel.send('One or more emojis failed to react!'));
const collector = await translateembed.createReactionCollector((reaction, user) => user == params.msg.author && (reaction.emoji.name == fromchnemoji || reaction.emoji.name == transfromemoji || reaction.emoji.name == transtoemoji), {time: 15000});
await collector.on('collect', r => {
collector.resetTimer( {time: 1} )
switch(r.emoji.name) {
case fromchnemoji:
process = "settofrom"
break;
case transfromemoji:
process = "translatefrom"
break;
case transtoemoji:
process = "translateto"
break;
}
});
function checkprocess () {
if (process != undefined) {
determineprocess(process)
}
else {
params.msg.channel.send('Closed due to no reponse')
}
}
collector.on('end', () => checkprocess());
});
@SuppliedOrange
Copy link
Author

Criticism is no longer welcome. This is horrible, I can't believe I made this 3 years ago.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment