mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
feat: use webhooks for logs when possible
This commit is contained in:
parent
1081d1b361
commit
55a39e0758
12 changed files with 318 additions and 29 deletions
|
@ -0,0 +1,48 @@
|
|||
import { GuildPluginData } from "knub";
|
||||
import { InternalPosterPluginType } from "../types";
|
||||
import { NewsChannel, Permissions, TextChannel } from "discord.js";
|
||||
import { isDiscordAPIError } from "../../../utils";
|
||||
|
||||
type WebhookInfo = [id: string, token: string];
|
||||
|
||||
export async function getOrCreateWebhookForChannel(
|
||||
pluginData: GuildPluginData<InternalPosterPluginType>,
|
||||
channel: TextChannel | NewsChannel,
|
||||
): Promise<WebhookInfo | null> {
|
||||
// tslint:disable-next-line:no-console FIXME: Here for debugging purposes
|
||||
console.log(`getOrCreateWebhookForChannel(${channel.id})`);
|
||||
|
||||
// Database cache
|
||||
const fromDb = await pluginData.state.webhooks.findByChannelId(channel.id);
|
||||
if (fromDb) {
|
||||
return [fromDb.id, fromDb.token];
|
||||
}
|
||||
|
||||
if (pluginData.state.missingPermissions) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Create new webhook
|
||||
const member = pluginData.client.user && pluginData.guild.members.cache.get(pluginData.client.user.id);
|
||||
if (!member || member.permissions.has(Permissions.FLAGS.MANAGE_WEBHOOKS)) {
|
||||
try {
|
||||
const webhook = await channel.createWebhook(`Zephook ${channel.id}`);
|
||||
await pluginData.state.webhooks.create({
|
||||
id: webhook.id,
|
||||
guild_id: pluginData.guild.id,
|
||||
channel_id: channel.id,
|
||||
token: webhook.token!,
|
||||
});
|
||||
return [webhook.id, webhook.token!];
|
||||
} catch (err) {
|
||||
if (isDiscordAPIError(err) && err.code === 50013) {
|
||||
pluginData.state.missingPermissions = true;
|
||||
console.warn(`Error ${err.code} when trying to create webhook for ${pluginData.guild.id}`);
|
||||
return null;
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue