2022-02-02 11:46:42 +02:00
|
|
|
import { EventEmitter } from "events";
|
2022-02-02 20:24:14 +02:00
|
|
|
import { wait } from "./common.js";
|
2022-02-01 03:49:12 +02:00
|
|
|
|
|
|
|
class WatchedGuild extends EventEmitter {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.knownWebhooks = new Map();
|
|
|
|
this.upstreamGuildId = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
pushEvent(e) {
|
|
|
|
this.emit("pushed", e);
|
|
|
|
}
|
|
|
|
|
2022-02-02 20:24:14 +02:00
|
|
|
holdForEvent(timeout) {
|
|
|
|
return new Promise((outerResolve, outerReject) => {
|
|
|
|
let handler;
|
|
|
|
Promise.race([
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
// potential memory leak here when too many promises are created and waiting
|
|
|
|
handler = (event) => {
|
|
|
|
resolve(event);
|
|
|
|
};
|
|
|
|
this.once("pushed", handler);
|
|
|
|
}),
|
|
|
|
wait(timeout, true)
|
|
|
|
]).then((result) => {
|
|
|
|
if (handler)
|
|
|
|
this.removeListener("pushed", handler);
|
|
|
|
outerResolve(result);
|
|
|
|
}).catch(() => {
|
|
|
|
if (handler)
|
|
|
|
this.removeListener("pushed", handler);
|
|
|
|
outerResolve(null);
|
2022-02-02 11:46:42 +02:00
|
|
|
});
|
|
|
|
});
|
2022-02-01 03:49:12 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 11:46:42 +02:00
|
|
|
discordConnect(bot) {
|
|
|
|
this.bot = bot;
|
|
|
|
this.guildObject = this.bot.guilds.find(e => e.id === this.upstreamGuildId);
|
2022-02-01 03:49:12 +02:00
|
|
|
|
2022-02-02 11:46:42 +02:00
|
|
|
if (!this.guildObject) {
|
|
|
|
throw new Error("Could not find guild object from bot cache by id (is the upstreamGuildId valid and does the bot have access to it?)");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.bot.on("GUILD_CREATE", (guild) => {
|
|
|
|
if (guild.id === this.upstreamGuildId)
|
|
|
|
this.guildObject = guild;
|
|
|
|
});
|
2022-02-01 03:49:12 +02:00
|
|
|
|
2022-02-02 11:46:42 +02:00
|
|
|
this.bot.on("GUILD_UPDATE", (guild) => {
|
|
|
|
if (guild.id === this.upstreamGuildId)
|
|
|
|
this.guildObject = guild;
|
2022-02-01 03:49:12 +02:00
|
|
|
});
|
|
|
|
|
2022-02-02 11:46:42 +02:00
|
|
|
this.bot.on("MESSAGE_CREATE", (message) => {
|
|
|
|
if (message.guild_id !== this.upstreamGuildId)
|
2022-02-01 03:49:12 +02:00
|
|
|
return;
|
|
|
|
|
2022-02-02 21:43:58 +02:00
|
|
|
const maybeKnownWebhook = this.knownWebhooks.get(message.channel_id);
|
2022-02-02 23:48:18 +02:00
|
|
|
if (maybeKnownWebhook && maybeKnownWebhook.id === message.webhook_id)
|
2022-02-02 21:43:58 +02:00
|
|
|
return; // ignore messages coming from our webhook
|
|
|
|
|
2022-02-02 11:46:42 +02:00
|
|
|
this.pushEvent({
|
|
|
|
eventType: "MESSAGE_CREATE",
|
|
|
|
message: message
|
|
|
|
});
|
2022-02-01 03:49:12 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-02 11:46:42 +02:00
|
|
|
userFacingChannelList() {
|
|
|
|
return this.guildObject.channels.map(channel => ({ id: channel.id, name: channel.name, position: channel.position, type: channel.type, nsfw: channel.nsfw }));
|
|
|
|
}
|
2022-02-01 03:49:12 +02:00
|
|
|
|
2022-02-02 11:46:42 +02:00
|
|
|
async discordSendMessage(content, channelId, username, avatarURL=undefined) {
|
2022-02-01 03:49:12 +02:00
|
|
|
let webhook = this.knownWebhooks.get(channelId);
|
|
|
|
if (!webhook) {
|
2022-02-02 11:46:42 +02:00
|
|
|
webhook = (await this.bot.api(["GET", `/channels/${channelId}/webhooks`]))
|
|
|
|
.find(e => e.name === "well_known__bridge");
|
|
|
|
|
2022-02-01 03:49:12 +02:00
|
|
|
if (!webhook)
|
2022-02-02 11:46:42 +02:00
|
|
|
webhook = await this.bot.api(["POST", `/channels/${channelId}/webhooks`], {
|
2022-02-01 03:49:12 +02:00
|
|
|
name: "well_known__bridge"
|
2022-02-02 11:46:42 +02:00
|
|
|
});
|
2022-02-01 03:49:12 +02:00
|
|
|
|
|
|
|
this.knownWebhooks.set(channelId, webhook);
|
|
|
|
}
|
|
|
|
|
2022-02-02 11:46:42 +02:00
|
|
|
await this.bot.api(["POST", `/webhooks/${webhook.id}/${webhook.token}?wait=true`], {
|
|
|
|
content,
|
|
|
|
username,
|
|
|
|
avatar_url: avatarURL,
|
2022-02-01 03:49:12 +02:00
|
|
|
tts: false,
|
2022-02-02 11:46:42 +02:00
|
|
|
allowed_mentions: {
|
|
|
|
parse: ["users"]
|
|
|
|
}
|
2022-02-01 03:49:12 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-02 11:46:42 +02:00
|
|
|
export default WatchedGuild;
|