Merge pull request #21 from keanuplayz/emote-dump

This commit is contained in:
Keanu Timmermans 2021-01-03 16:27:45 +01:00 committed by GitHub
commit 56dafed616
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 92 additions and 2 deletions

2
.gitignore vendored
View File

@ -68,4 +68,4 @@ typings/
.env
config.json
.vscode/
.vscode/

View File

@ -5,6 +5,7 @@ import {get} from "https";
import FileManager from "./storage";
import {eventListeners} from "../events/messageReactionRemove";
import {client} from "../index";
import {EmoteRegistryDump, EmoteRegistryDumpEntry} from "./structures";
/** A type that describes what the library module does. */
export interface CommonLibrary {
@ -160,6 +161,27 @@ export function botHasPermission(guild: Guild | null, permission: number): boole
return !!(client.user && guild?.members.resolve(client.user)?.hasPermission(permission));
}
export function updateGlobalEmoteRegistry(): void {
const data: EmoteRegistryDump = {version: 1, list: []};
for (const guild of client.guilds.cache.values()) {
for (const emote of guild.emojis.cache.values()) {
data.list.push({
ref: emote.name,
id: emote.id,
name: emote.name,
requires_colons: emote.requiresColons || false,
animated: emote.animated,
url: emote.url,
guild_id: emote.guild.name,
guild_name: emote.guild.name
});
}
}
FileManager.write("emote-registry", data, true);
}
// Pagination function that allows for customization via a callback.
// Define your own pages outside the function because this only manages the actual turning of pages.
$.paginate = async (

View File

@ -1,7 +1,7 @@
import FileManager from "./storage";
import $, {select, GenericJSON, GenericStructure} from "./lib";
import {watch} from "fs";
import {Guild as DiscordGuild} from "discord.js";
import {Guild as DiscordGuild, Snowflake} from "discord.js";
class ConfigStructure extends GenericStructure {
public token: string;
@ -106,3 +106,19 @@ if (process.argv[2] === "dev") {
export function getPrefix(guild: DiscordGuild | null): string {
return Storage.getGuild(guild?.id || "N/A").prefix ?? Config.prefix;
}
export interface EmoteRegistryDumpEntry {
ref: string;
id: Snowflake;
name: string;
requires_colons: boolean;
animated: boolean;
url: string;
guild_id: Snowflake;
guild_name: string;
}
export interface EmoteRegistryDump {
version: number;
list: EmoteRegistryDumpEntry[];
}

10
src/events/emojiCreate.ts Normal file
View File

@ -0,0 +1,10 @@
import Event from "../core/event";
import $ from "../core/lib";
import {updateGlobalEmoteRegistry} from "../core/lib";
export default new Event<"emojiCreate">({
on(emote) {
$.log(`Updated emote registry. ${emote.name}`);
updateGlobalEmoteRegistry();
}
});

10
src/events/emojiDelete.ts Normal file
View File

@ -0,0 +1,10 @@
import Event from "../core/event";
import $ from "../core/lib";
import {updateGlobalEmoteRegistry} from "../core/lib";
export default new Event<"emojiDelete">({
on() {
$.log("Updated emote registry.");
updateGlobalEmoteRegistry();
}
});

10
src/events/emojiUpdate.ts Normal file
View File

@ -0,0 +1,10 @@
import Event from "../core/event";
import $ from "../core/lib";
import {updateGlobalEmoteRegistry} from "../core/lib";
export default new Event<"emojiUpdate">({
on() {
$.log("Updated emote registry.");
updateGlobalEmoteRegistry();
}
});

10
src/events/guildCreate.ts Normal file
View File

@ -0,0 +1,10 @@
import Event from "../core/event";
import $ from "../core/lib";
import {updateGlobalEmoteRegistry} from "../core/lib";
export default new Event<"guildCreate">({
on() {
$.log("Updated emote registry.");
updateGlobalEmoteRegistry();
}
});

10
src/events/guildDelete.ts Normal file
View File

@ -0,0 +1,10 @@
import Event from "../core/event";
import $ from "../core/lib";
import {updateGlobalEmoteRegistry} from "../core/lib";
export default new Event<"guildDelete">({
on() {
$.log("Updated emote registry.");
updateGlobalEmoteRegistry();
}
});

View File

@ -2,6 +2,7 @@ import Event from "../core/event";
import {client} from "../index";
import $ from "../core/lib";
import {Config} from "../core/structures";
import {updateGlobalEmoteRegistry} from "../core/lib";
export default new Event<"ready">({
once() {
@ -12,5 +13,6 @@ export default new Event<"ready">({
name: `${Config.prefix}help`
});
}
updateGlobalEmoteRegistry();
}
});