Skip to content

Instantly share code, notes, and snippets.

@Nama
Last active February 1, 2024 19:24
Show Gist options
  • Save Nama/e61a13c29717b8eff48fa3ff41fc4ddd to your computer and use it in GitHub Desktop.
Save Nama/e61a13c29717b8eff48fa3ff41fc4ddd to your computer and use it in GitHub Desktop.
TwitFix link instead of twitter with betterdiscord
/**
* @name TwitFix
* @version 1.5
* @authorLink https://github.com/Nama
* @website https://yamahi.eu
* @description Send twitter links with preview
* @source https://gist.github.com/Nama/e61a13c29717b8eff48fa3ff41fc4ddd
* @updateUrl https://gist.githubusercontent.com/Nama/e61a13c29717b8eff48fa3ff41fc4ddd/raw/
*/
/*
* Replaces all occurrences of twitter.com in URLs if it is a link to a post (contains status)
* Removes all parameters and gets rid of mobile subdomain
* Suffix the URL with the amount of images (eg. /3) and all images will be posted
* Suffix the URL with /c to get a combined image perview (c.vxtwitter.com)
* Suffix the URL with /t to not use vxtwitter
* vxtiktok.com
*/
var TwitFix = (() => {
function start() {
let matchurls = /(https:\/\/vxtwitter\.com\/[\w]+\/status\/\d+)(?:\?s=20)?(\/[ct\d])?/g;
let matchurl = /(https:\/\/vxtwitter\.com\/[\w]+\/status\/\d+)(\/[ct\d])?/g;
BdApi.Patcher.before('TwitFix', BdApi.Webpack.getModule(BdApi.Webpack.Filters.byProps('sendMessage')), 'sendMessage', (_,props,ret)=> {
let method = 'sendMessage'
let newmsg = props[1].content;
replace_url(method, props, newmsg)
})
BdApi.Patcher.before('TwitFix', BdApi.Webpack.getModule(BdApi.Webpack.Filters.byProps('uploadFiles')), 'uploadFiles', (_,props,ret)=> {
let method = 'uploadFiles'
let newmsg = props[0].parsedMessage.content;
replace_url(method, props, newmsg)
})
function replace_url(method, props, newmsg) {
//console.log(newmsg)
newmsg = newmsg.replaceAll('https://vm.tiktok.com', 'https://vm.vxtiktok.com');
newmsg = newmsg.replaceAll('https://www.tiktok.com', 'https://www.vxtiktok.com');
newmsg = newmsg.replaceAll('https://mobile.twitter.com', 'https://twitter.com');
if (newmsg.includes('twitter.com') || newmsg.includes('x.com') && newmsg.includes('status')) {
newmsg = newmsg.replaceAll('https://twitter.com', 'https://vxtwitter.com');
newmsg = newmsg.replaceAll('https://x.com', 'https://vxtwitter.com');
newmsg = newmsg.replaceAll(matchurls, '$1$2');
const urls = [...newmsg.matchAll(matchurl)];
// [0] vxtwitter URL
// [1] eg. /4 suffix
//console.log(urls[0][1])
for (let i = 0; i < urls.length; i++) {
if (urls[i][2]) {
let url = urls[i][1];
let suffix = urls[i][2].replace('/', '');
let newurl;
if (!suffix) {
newurl = url
}
else if (suffix === 'c') {
newurl = url.replace('https://vxtwitter.com', 'https://c.vxtwitter.com')
}
else if (suffix === 't') {
newurl = url.replace('https://vxtwitter.com', 'https://twitter.com')
}
else {
suffix = parseInt(suffix);
newurl = url + '/1\n'; // else "undefined" is in the message
for (let m = 2; m <= suffix; m++) {
newurl += url + '/' + m + '\n';
}
}
newmsg = newmsg.replace(urls[i][0], newurl);
}
}
}
if (method === 'sendMessage') {
props[1].content = newmsg;
}
else {
props[0].parsedMessage.content = newmsg;
}
}
}
function stop() {
BdApi.Patcher.unpatchAll('TwitFix');
}
return function() { return {
getName: () => 'TwitFix',
getShortName: () => 'TwitFix',
getDescription: () => 'Send twitter-links with better preview',
getVersion: () => '1.5',
getAuthor: () => 'Yama',
start: start,
stop: stop
}};
})();
module.exports = TwitFix;
@RoyRiv3r
Copy link

Hi, have you find a way to change the url whenever there is an attachment to it? Seems like it's using different module whenever there is file involved within message

@Nama
Copy link
Author

Nama commented Jan 11, 2024

Hi @RoyRiv3r
either I forgot about the issue, or never found out. I just made it work and also got rid of the deprecated methods.

How did you found out about this plugin? I thought nobody is using it XD

@RoyRiv3r
Copy link

Lol yeah, i was looking for a way to send twitter link with the embed since it's not working on discord anymore, i made my own https://github.com/RoyRiv3r/TwitterX.plugin.js/blob/main/TwitterX.plugin.js
Then I discovered later that whenever there is an attachment file to the message with url it doesn't use the same module it uses uploadFiles? so i just searched to see if there is any solution and i found your code. 👍

@Nama
Copy link
Author

Nama commented Jan 11, 2024

ah lol.

Ya, I don't know much JS and even less BD, so I searched their discord for messages in that regard to hopefully find something.
And I did. uploadFiles is the module.
The docs don't really help, since these only have BD things, not discord things.

@Nama
Copy link
Author

Nama commented Feb 1, 2024

You can use /2, /3, etc. without removing the ?s=20 parameter.

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