Skip to content

Commit

Permalink
feat: discord links importer
Browse files Browse the repository at this point in the history
  • Loading branch information
kimlimjustin committed Sep 15, 2024
1 parent 174e599 commit 066f529
Show file tree
Hide file tree
Showing 12 changed files with 369 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .env.local
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ SMTP_SECURE="false"
SMTP_USER="dummy"
SMTP_PASS="dummy"

DISCORD_TOKEN=

TRAEFIK_CONFIG_SERVICE_NAME="srv-weird@docker"
TRAEFIK_CONFIG_NAMESPACE="weird-one-dynamic-config"
PUBLIC_TRAEFIK_CONFIG_HOST="127.0.0.1:9523"
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"autoprefixer": "^10.4.20",
"cookie": "^0.6.0",
"date-fns": "^3.6.0",
"discord.js": "^14.16.2",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-svelte": "^2.43.0",
Expand All @@ -56,7 +57,8 @@
"vite": "^5.4.3",
"vite-plugin-top-level-await": "^1.4.4",
"vite-plugin-wasm": "^3.3.0",
"ws": "^8.18.0"
"ws": "^8.18.0",
"zlib-sync": "^0.1.9"
},
"dependencies": {
"sharp": "^0.33.5"
Expand Down
167 changes: 167 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import {client_login} from './lib/discord_bot/index.server.';
client_login()
55 changes: 55 additions & 0 deletions src/lib/discord_bot/index.server..ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Discord from 'discord.js';
import {GatewayIntentBits} from 'discord.js';
import { env } from '$env/dynamic/private';
import {env as PublicEnv} from '$env/dynamic/public';
import { getProfileById, setProfileById } from '$lib/leaf/profile';
import { leafClient } from '$lib/leaf';

const client = new Discord.Client({intents: [GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent]});

client.on('ready', () => {
console.log(`Logged in as ${client.user!.tag}!`);
});

const ExtractUrlName = (url: string) => {
return url.split('/').slice(-1)[0];
}

client.on("messageCreate", async function (message) {
if (message.content === "/weird_auth") {
message.author.send(`Please go to this link to authenticate: ${env.APP_URL}/app/discord_bot_authenticator?q=${message.author.id}`);
message.reply("I have sent you a DM with the link to authenticate.");
} else if (message.content.startsWith("/weird_import_links")){
const links = message.content.split(" ").slice(1);
const discord_tokens = JSON.parse(await leafClient.get_local_secret("discord_tokens") ?? "{}");
const userId = Object.keys(discord_tokens).find(key => discord_tokens[key] === message.author.id) as string;
let profile = await getProfileById(userId);
if(!profile){
message.reply("You need to authenticate first. Use the command `/weird_auth` to authenticate.");
return;
}
let edited = false;
for(let list of profile['lists']){
if(list.label === 'discord_links'){
list.links = [...list.links, ...links.map((link) => ({label: ExtractUrlName(link), url: link}))];
edited = true;
}
}
if(!edited){
profile['lists'].push({label: 'discord_links', links: links.map((link) => ({label: ExtractUrlName(link), url: link}))});
}
try{
await setProfileById(userId, profile!);
}catch(e){
console.log('error', e)
}
message.reply(`Links imported successfully (http://${PublicEnv.PUBLIC_DOMAIN}/u/${profile.username}/discord_links).`);
}
});


export const client_login = () => client.login(env.DISCORD_TOKEN);

client.on('messageCreate', msg => console.log(msg.content));
Loading

0 comments on commit 066f529

Please sign in to comment.