aboutsummaryrefslogtreecommitdiff
path: root/utils.js
diff options
context:
space:
mode:
authorJacob Janzen <jacob@jjanzen.ca>2024-12-08 01:56:35 +0000
committerJacob Janzen <jacob@jjanzen.ca>2024-12-08 01:56:35 +0000
commit0fbf84b271709582fb328941382f563d15e129eb (patch)
treee6b142684e092b54dd036f04a714b3944270c00e /utils.js
initial commit
Diffstat (limited to 'utils.js')
-rw-r--r--utils.js47
1 files changed, 47 insertions, 0 deletions
diff --git a/utils.js b/utils.js
new file mode 100644
index 0000000..506aea6
--- /dev/null
+++ b/utils.js
@@ -0,0 +1,47 @@
+import 'dotenv/config';
+
+export async function DiscordRequest(endpoint, options) {
+ // append endpoint to root API URL
+ const url = 'https://discord.com/api/v10/' + endpoint;
+ // Stringify payloads
+ if (options.body) options.body = JSON.stringify(options.body);
+ // Use fetch to make requests
+ const res = await fetch(url, {
+ headers: {
+ Authorization: `Bot ${process.env.DISCORD_TOKEN}`,
+ 'Content-Type': 'application/json; charset=UTF-8',
+ 'User-Agent': 'DiscordBot (https://github.com/discord/discord-example-app, 1.0.0)',
+ },
+ ...options
+ });
+ // throw API errors
+ if (!res.ok) {
+ const data = await res.json();
+ console.log(res.status);
+ throw new Error(JSON.stringify(data));
+ }
+ // return original response
+ return res;
+}
+
+export async function InstallGlobalCommands(appId, commands) {
+ // API endpoint to overwrite global commands
+ const endpoint = `applications/${appId}/commands`;
+
+ try {
+ // This is calling the bulk overwrite endpoint: https://discord.com/developers/docs/interactions/application-commands#bulk-overwrite-global-application-commands
+ await DiscordRequest(endpoint, { method: 'PUT', body: commands });
+ } catch (err) {
+ console.error(err);
+ }
+}
+
+// Simple method that returns a random emoji from list
+export function getRandomEmoji() {
+ const emojiList = ['😭','πŸ˜„','😌','πŸ€“','😎','😀','πŸ€–','πŸ˜Άβ€πŸŒ«οΈ','🌏','πŸ“Έ','πŸ’Ώ','πŸ‘‹','🌊','✨'];
+ return emojiList[Math.floor(Math.random() * emojiList.length)];
+}
+
+export function capitalize(str) {
+ return str.charAt(0).toUpperCase() + str.slice(1);
+}