update/webhook handlers
This commit is contained in:
3 files changed
+59
-2
No files matched your search
+2
-1
File diff suppressed because one or more lines are too long.
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "dayzr-bot",
|
"name": "dayzr-bot",
|
||||||
"version": "13.3.15",
|
"version": "13.3.16",
|
||||||
"description": "A General Purpose Discord Bot for DayZ Nitrado Servers.",
|
"description": "A General Purpose Discord Bot for DayZ Nitrado Servers.",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"nodemonConfig": {
|
"nodemonConfig": {
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
const { makeURLSearchParams } = require('@discordjs/rest');
|
||||||
|
const { REST } = require('@discordjs/rest');
|
||||||
|
const { Routes } = require('discord.js');
|
||||||
|
|
||||||
|
const createWebhook = async (client, channel_id, name, avatar) => {
|
||||||
|
const rest = new REST({ version: '10' }).setToken(client.config.Token);
|
||||||
|
return await rest.post(Routes.channelWebhooks(channel_id), {
|
||||||
|
body: {
|
||||||
|
name: name,
|
||||||
|
avatar: avatar
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
GetWebhook: async (client, webhookName, channel_id) => {
|
||||||
|
// Get all webhooks from configured channel
|
||||||
|
const rest = new REST({ version: '10' }).setToken(client.config.Token);
|
||||||
|
const webhooks = await rest.get(Routes.channelWebhooks(channel_id));
|
||||||
|
|
||||||
|
let webhook = null;
|
||||||
|
if (webhooks.length == 0) {
|
||||||
|
// If no webhook exists, create new webhook with given name for this channel
|
||||||
|
webhook = createWebhook(client, channel_id, webhookName, client.config.AvatarData);
|
||||||
|
} else {
|
||||||
|
// Check existing webhooks for one with given name
|
||||||
|
let exists = false;
|
||||||
|
for (let i = 0; i < webhooks.length; i++) {
|
||||||
|
if (webhooks[i].name == webhookName) {
|
||||||
|
webhook = webhooks[i];
|
||||||
|
exists = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!exists) webhook = createWebhook(client, channel_id, webhookName, client.config.AvatarData);
|
||||||
|
}
|
||||||
|
|
||||||
|
return webhook;
|
||||||
|
},
|
||||||
|
|
||||||
|
WebhookSend: async (client, webhook, content) => {
|
||||||
|
const rest = new REST({ version: '10' }).setToken(client.config.Token);
|
||||||
|
return await rest.post(Routes.webhook(webhook.id, webhook.token), {
|
||||||
|
body: content,
|
||||||
|
query: makeURLSearchParams({ wait: true })
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
WebhookMessageEdit: async (client, webhook, message_id, content) => {
|
||||||
|
const rest = new REST({ version: '10' }).setToken(client.config.Token);
|
||||||
|
return rest.patch(Routes.webhookMessage(webhook.id, webhook.token, message_id), {
|
||||||
|
body: content
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user