3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 20:35:02 +00:00

Update to discord.js v13.8.0, adding support for text-in-voice

This commit is contained in:
Dragory 2022-06-13 21:19:56 +03:00
parent ba78103807
commit b05fbe1d04
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
32 changed files with 151 additions and 149 deletions

View file

@ -1,11 +1,11 @@
import { GuildPluginData } from "knub";
import { InternalPosterPluginType } from "../types";
import { NewsChannel, TextChannel, WebhookClient } from "discord.js";
import { getOrCreateWebhookForChannel } from "./getOrCreateWebhookForChannel";
import { WebhookClient } from "discord.js";
import { getOrCreateWebhookForChannel, WebhookableChannel } from "./getOrCreateWebhookForChannel";
export async function getOrCreateWebhookClientForChannel(
pluginData: GuildPluginData<InternalPosterPluginType>,
channel: TextChannel | NewsChannel,
channel: WebhookableChannel,
): Promise<WebhookClient | null> {
if (!pluginData.state.webhookClientCache.has(channel.id)) {
const webhookInfo = await getOrCreateWebhookForChannel(pluginData, channel);

View file

@ -1,17 +1,20 @@
import { GuildPluginData } from "knub";
import { InternalPosterPluginType } from "../types";
import { NewsChannel, Permissions, TextChannel } from "discord.js";
import { AnyChannel, GuildChannel, MessageManager, NewsChannel, Permissions, TextChannel } from "discord.js";
import { isDiscordAPIError } from "../../../utils";
type WebhookInfo = [id: string, token: string];
export type WebhookableChannel = Extract<AnyChannel, { createWebhook: (...args: any[]) => any }>;
export function channelIsWebhookable(channel: AnyChannel): channel is WebhookableChannel {
return "createWebhook" in channel;
}
export async function getOrCreateWebhookForChannel(
pluginData: GuildPluginData<InternalPosterPluginType>,
channel: TextChannel | NewsChannel,
channel: WebhookableChannel,
): 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) {

View file

@ -1,7 +1,7 @@
import { Message, MessageOptions, NewsChannel, TextChannel, WebhookClient } from "discord.js";
import { GuildTextBasedChannel, Message, MessageOptions, NewsChannel, TextChannel, WebhookClient } from "discord.js";
import { GuildPluginData } from "knub";
import { InternalPosterPluginType } from "../types";
import { getOrCreateWebhookForChannel } from "./getOrCreateWebhookForChannel";
import { channelIsWebhookable, getOrCreateWebhookForChannel } from "./getOrCreateWebhookForChannel";
import { APIMessage } from "discord-api-types";
import { isDiscordAPIError } from "../../../utils";
import { getOrCreateWebhookClientForChannel } from "./getOrCreateWebhookClientForChannel";
@ -12,7 +12,7 @@ export type InternalPosterMessageResult = {
};
async function sendDirectly(
channel: TextChannel | NewsChannel,
channel: GuildTextBasedChannel,
content: MessageOptions,
): Promise<InternalPosterMessageResult | null> {
return channel.send(content).then((message) => ({
@ -26,17 +26,26 @@ async function sendDirectly(
*/
export async function sendMessage(
pluginData: GuildPluginData<InternalPosterPluginType>,
channel: TextChannel | NewsChannel,
channel: GuildTextBasedChannel,
content: MessageOptions,
): Promise<InternalPosterMessageResult | null> {
return pluginData.state.queue.add(async () => {
const webhookClient = await getOrCreateWebhookClientForChannel(pluginData, channel);
let webhookClient: WebhookClient | null = null;
let threadId: string | undefined;
if (channelIsWebhookable(channel)) {
webhookClient = await getOrCreateWebhookClientForChannel(pluginData, channel);
} else if (channel.isThread() && channelIsWebhookable(channel.parent!)) {
webhookClient = await getOrCreateWebhookClientForChannel(pluginData, channel.parent!);
threadId = channel.id;
}
if (!webhookClient) {
return sendDirectly(channel, content);
}
return webhookClient
.send({
threadId,
...content,
...(pluginData.client.user && {
username: pluginData.client.user.username,
@ -50,7 +59,7 @@ export async function sendMessage(
.catch(async (err) => {
// Unknown Webhook
if (isDiscordAPIError(err) && err.code === 10015) {
await pluginData.state.webhooks.delete(webhookClient.id);
await pluginData.state.webhooks.delete(webhookClient!.id);
pluginData.state.webhookClientCache.delete(channel.id);
// Fallback to regular message for this log message