mirror of
https://github.com/pieartsy/PluralFlux.git
synced 2026-04-16 17:45:28 +10:00
discord js (#1)
* clear out fluxer.net * basic discord.js bot with ping, and echo * added creation of webhook * simplifying webhook logic * sending messages as webhook * deleting orignal message * adding sequelize file * commented out pgadmin part while it's not working * adding member sort of working * changing names of values * updating names of values and adding new method in memberHelper * renamed messagehelper function * deleted proxyhelper * passed only channel id into webhook helper methods * added new functions and got update working * changed message match to better reducer * adjusted webhook helper to use actual data * refactored bot.js and removed unused methods
This commit is contained in:
37
helpers/webhookHelper.js
Normal file
37
helpers/webhookHelper.js
Normal file
@@ -0,0 +1,37 @@
|
||||
const wh = {};
|
||||
|
||||
wh.get_or_create_webhook = async function (api, channel_id) {
|
||||
const name = 'PluralFlux Proxy Webhook';
|
||||
let webhook = await get_webhook(api, channel_id, name);
|
||||
if (webhook === undefined) {
|
||||
webhook = await api.channels.createWebhook(channel_id, {name: name});
|
||||
}
|
||||
return webhook;
|
||||
}
|
||||
|
||||
async function get_webhook(api, channel_id, name) {
|
||||
const all_webhooks = await api.channels.getWebhooks(channel_id);
|
||||
if (all_webhooks.length === 0) {
|
||||
return;
|
||||
}
|
||||
let pf_webhook;
|
||||
all_webhooks.forEach((webhook) => {
|
||||
if (webhook.name === name) {
|
||||
pf_webhook = webhook;
|
||||
}
|
||||
})
|
||||
return pf_webhook;
|
||||
}
|
||||
|
||||
wh.replace_message = async function (api, data, text, member) {
|
||||
if (text.length > 0) {
|
||||
const webhook = await wh.get_or_create_webhook(api, data.channel_id);
|
||||
await api.webhooks.execute(webhook.id, webhook.token, {content: text, username: member.displayname ?? member.name, propic: member.propic});
|
||||
await api.channels.deleteMessage(data.channel_id, data.id);
|
||||
}
|
||||
else {
|
||||
await api.channels.createMessage(data.channel_id, {content: '(Please input a message!)'});
|
||||
}
|
||||
}
|
||||
|
||||
export const webhookHelper = wh;
|
||||
Reference in New Issue
Block a user