-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simplify and improve command and event registration
- Loading branch information
Showing
11 changed files
with
121 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { ChannelType, Guild as djsGuild, NonThreadGuildBasedChannel } from "discord.js" | ||
|
||
type NonThreadGuildBasedChannelType = | ||
| ChannelType.GuildText | ||
| ChannelType.GuildVoice | ||
| ChannelType.GuildNews | ||
| ChannelType.GuildStageVoice | ||
| ChannelType.GuildCategory | ||
|
||
class Guild { | ||
readonly id: string | ||
readonly guild: djsGuild | ||
|
||
constructor(id: string, guild: djsGuild) { | ||
this.id = id | ||
this.guild = guild | ||
} | ||
|
||
get members() { | ||
return this.guild.members.fetch() | ||
} | ||
|
||
get channels() { | ||
return this.guild.channels.fetch() | ||
} | ||
|
||
get roles() { | ||
return this.guild.roles.fetch() | ||
} | ||
|
||
get emojis() { | ||
return this.guild.emojis.fetch() | ||
} | ||
|
||
async getChannel<T extends NonThreadGuildBasedChannel>( | ||
channelNameOrId: string, | ||
channelType: NonThreadGuildBasedChannelType, | ||
): Promise<T | undefined> { | ||
const channels = await this.channels | ||
|
||
let channel: NonThreadGuildBasedChannel | undefined | null | ||
channel = channels.find((channel) => (channel ? channel.name === channelNameOrId : false)) | ||
if (channel) return channel.type === channelType ? (channel as T) : undefined | ||
|
||
channel = channels.get(channelNameOrId) | ||
if (channel) return channel.type === channelType ? (channel as T) : undefined | ||
} | ||
} | ||
|
||
export default Guild |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,37 @@ | ||
import { Client, ClientOptions } from "discord.js" | ||
import registerCommands from "./commands.js" | ||
import registerEvents from "./events.js" | ||
import { setBot } from "./guild-cache.js" | ||
import registerInteractionCreate, { InteractionCheck } from "./interaction-create.js" | ||
import registerReady from "./ready.js" | ||
import { Client, ClientOptions, Collection } from "discord.js" | ||
import registerInteractionCreate, { InteractionCheck } from "./events/interaction-create.js" | ||
import registerReady from "./events/ready.js" | ||
import Guild from "./guild.js" | ||
import registerCommands, { Commands } from "./register-commands.js" | ||
import registerEvents, { Events } from "./register-events.js" | ||
|
||
const botToken = process.env.BOT_TOKEN || "" | ||
const clientId = process.env.CLIENT_ID || "" | ||
const guildId = process.env.GUILD_ID || "" | ||
|
||
async function login(botIntents: ClientOptions, projectMetaURL: string, interactionCheck?: InteractionCheck): Promise<Client> { | ||
async function login(botIntents: ClientOptions, commands: Commands, events: Events, interactionCheck?: InteractionCheck) { | ||
console.log("Logging in...") | ||
|
||
const bot = new Client(botIntents) | ||
const commands = await registerCommands(botToken, clientId, projectMetaURL, guildId) | ||
await registerCommands(botToken, clientId, commands) | ||
registerEvents(bot, events) | ||
|
||
// Register default/built-in events | ||
registerReady(bot) | ||
registerInteractionCreate(bot, commands, interactionCheck) | ||
void registerEvents(projectMetaURL) | ||
|
||
setBot(bot) | ||
await bot.login(botToken) | ||
console.log("Logged in.") | ||
|
||
const partialGuilds = await bot.guilds.fetch() | ||
const guildPromises = partialGuilds.map((guildPartial) => guildPartial.fetch()) | ||
const guilds = await Promise.all(guildPromises) | ||
const GuildCollection = new Collection<string, Guild>() | ||
for (const guild of guilds) GuildCollection.set(guild.id, new Guild(guild.id, guild)) | ||
|
||
void bot.login(botToken) | ||
return bot | ||
return { GuildCollection, bot } | ||
} | ||
|
||
export default login | ||
export { Command } from "./commands.js" | ||
export { default as getGuildCache } from "./guild-cache.js" | ||
export { InteractionCheck } from "./interaction-create.js" | ||
export { getChannel, isCategoryChannel, isTextChannel, throwError } from "./util.js" | ||
export { InteractionCheck } from "./events/interaction-create.js" | ||
export { Command } from "./register-commands.js" | ||
export { isCategoryChannel, isTextChannel, throwError } from "./util.js" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { REST } from "@discordjs/rest" | ||
import { Routes } from "discord-api-types/v10" | ||
import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js" | ||
|
||
interface Command { | ||
requiredRoles?: string[] | ||
command: SlashCommandBuilder | ||
run: (interaction: ChatInputCommandInteraction) => void | Promise<void> | ||
} | ||
|
||
interface Commands { | ||
[name: string]: Command | ||
} | ||
|
||
async function registerCommands(botToken: string, clientId: string, commands: Commands) { | ||
const commandData = Object.values(commands).map((command) => command.command.toJSON()) | ||
|
||
const rest = new REST().setToken(botToken) | ||
await rest.put(Routes.applicationCommands(clientId), { body: commandData }) | ||
console.log("Registered (/) commands.") | ||
} | ||
|
||
export default registerCommands | ||
export { Command, Commands } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Client } from "discord.js" | ||
|
||
type Event = (bot: Client) => void | ||
|
||
interface Events { | ||
[name: string]: Event | ||
} | ||
|
||
function registerEvents(bot: Client, events: Events) { | ||
for (const registerEvent of Object.values(events)) registerEvent(bot) | ||
console.log("Registered events.") | ||
} | ||
|
||
export default registerEvents | ||
export { Event, Events } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters