Skip to content

Commit

Permalink
replace channel type guards with getChannelByName function
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhl8 committed Aug 21, 2022
1 parent d45e606 commit 9d5781a
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"printWidth": 120,
"printWidth": 140,
"semi": false,
"singleQuote": true,
"trailingComma": "all"
Expand Down
248 changes: 124 additions & 124 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "discord-bot-shared",
"version": "0.3.4",
"version": "0.4.0",
"type": "module",
"description": "Modules for creating discord bots.",
"repository": "github:adamhl8/discord-bot-shared",
Expand All @@ -19,24 +19,24 @@
},
"dependencies": {
"@discordjs/rest": "^1.0.1",
"discord-api-types": "^0.37.2",
"discord-api-types": "^0.37.3",
"discord.js": "^14.2.0"
},
"devDependencies": {
"@types/eslint": "^8.4.5",
"@types/node": "^18.7.2",
"@types/eslint": "^8.4.6",
"@types/node": "^18.7.9",
"@types/prettier": "^2.7.0",
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0",
"eslint": "^8.21.0",
"@typescript-eslint/eslint-plugin": "^5.33.1",
"@typescript-eslint/parser": "^5.33.1",
"eslint": "^8.22.0",
"eslint-config-prettier": "^8.5.0",
"eslint-formatter-pretty": "^4.1.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-sonarjs": "^0.15.0",
"eslint-plugin-unicorn": "^43.0.2",
"prettier": "^2.7.1",
"prettier-plugin-organize-imports": "^3.0.3",
"prettier-plugin-pkg": "^0.16.1",
"prettier-plugin-organize-imports": "^3.1.0",
"prettier-plugin-pkg": "^0.17.0",
"prettier-plugin-sh": "^0.12.8",
"typescript": "^4.7.4"
}
Expand Down
6 changes: 3 additions & 3 deletions src/guildCache.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Client, Guild } from 'discord.js'
import { Client } from 'discord.js'

let bot: Client

function setBot(botClient: Client) {
bot = botClient
}

async function getGuildCache(baseGuild?: Guild) {
async function getGuildCache() {
if (!bot) return

const guilds = await bot.guilds.fetch()
if (!guilds) return
const guild = baseGuild ? await baseGuild.fetch() : await guilds.first()?.fetch()
const guild = await guilds.first()?.fetch()
if (!guild) return

const channels = await guild.channels.fetch()
Expand Down
8 changes: 2 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ 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, projectMetaURL: string, interactionCheck?: InteractionCheck): Promise<Client> {
const bot = new Client(botIntents)
const commands = await registerCommands(botToken, clientId, projectMetaURL, guildId)

Expand All @@ -31,4 +27,4 @@ export default login
export { Command } from './commands.js'
export { default as getGuildCache } from './guildCache.js'
export { InteractionCheck } from './interactionCreate.js'
export { isCategoryChannel, isTextChannel, throwError } from './util.js'
export { getChannelByName, throwError } from './util.js'
4 changes: 1 addition & 3 deletions src/interactionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ async function checkRoles(command: Command, interaction: ChatInputCommandInterac
const member = await interaction.guild?.members.fetch(interaction.user).catch(console.error)
if (!member) return

return member.roles.cache.some((role) =>
command.requiredRoles ? command.requiredRoles.includes(role.name) : false,
)
return member.roles.cache.some((role) => (command.requiredRoles ? command.requiredRoles.includes(role.name) : false))
}

return false
Expand Down
24 changes: 17 additions & 7 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { APIPartialChannel, BaseChannel, CategoryChannel, ChannelType, TextChannel } from 'discord.js'
import { ChannelType, NonThreadGuildBasedChannel } from 'discord.js'
import getGuildCache from './guildCache.js'

function isTextChannel(channel: BaseChannel | APIPartialChannel): channel is TextChannel {
return channel.type === ChannelType.GuildText
}
type NonThreadGuildBasedChannelType =
| ChannelType.GuildText
| ChannelType.GuildVoice
| ChannelType.GuildNews
| ChannelType.GuildStageVoice
| ChannelType.GuildCategory

async function getChannelByName<T extends NonThreadGuildBasedChannel>(
channelName: string,
channelType: NonThreadGuildBasedChannelType,
): Promise<T | undefined> {
const { channels } = (await getGuildCache()) || throwError('Unable to get guild cache.')
const channel = channels.find((channel) => channel.name === channelName)

function isCategoryChannel(channel: BaseChannel): channel is CategoryChannel {
return channel.type === ChannelType.GuildCategory
return channel && channel.type === channelType ? (channel as T) : undefined
}

function throwError(error: string): never {
throw new Error(error)
}

export { isTextChannel, isCategoryChannel, throwError }
export { getChannelByName, throwError }

0 comments on commit 9d5781a

Please sign in to comment.