-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
174e599
commit 066f529
Showing
12 changed files
with
369 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
Oops, something went wrong.