Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nethriis committed May 6, 2024
0 parents commit fb73bda
Show file tree
Hide file tree
Showing 22 changed files with 4,478 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Dependencies
node_modules
jspm_packages

package-lock.json
packages/*/README.md
!packages/harmonix/README.md
packages/*/LICENSE
*/**/yarn.lock
/.yarn

# Logs
*.log

# Temp directories
.temp
.tmp
.cache

# Generated dirs
dist
.harmony
.harmony-*
.output
.output-*
.gen

# Junit reports
reports

# Coverage reports
coverage
*.lcov
.nyc_output

# VSCode
.vscode

# Intellij idea
*.iml
.idea

.env
.pnpm-store
122 changes: 122 additions & 0 deletions bin/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/usr/bin/env node

import { loadCommands, loadEvents, loadHarmonyConfig } from '../dist/index.mjs'
import {
Client,
Collection,
SlashCommandBuilder,
REST,
Routes
} from 'discord.js'
import 'dotenv/config'

const commands = loadCommands()
const events = loadEvents()

const client = new Client({
intents: ['Guilds', 'GuildMessages', 'MessageContent']
})

client.commands = new Collection()
console.log('commands', commands)
console.log('events', events)

for (const command in commands) {
const { slash, data, execute } = commands[command]()

if (slash || data.slash) {
client.commands.set(command, {
data: new SlashCommandBuilder()
.setName(command)
.setDescription(data.description),
slash: slash || data.slash,
execute
})
} else {
client.commands.set(command, {
data,
slash: false,
execute
})
}
}

for (const event in events) {
const { once, data, callback } = events[event]()

if (once || data.once) {
client.once(event, callback)
} else {
client.on(event, callback)
}
}

const rest = new REST().setToken(process.env.CLIENT_TOKEN)

const refreshSlashCommands = async () => {
try {
console.log('Started refreshing application (/) commands.')

const commands = client.commands
.filter((command) => command.slash)
.map(({ data }) => data.toJSON())

await rest.put(Routes.applicationCommands(process.env.CLIENT_ID), {
body: commands
})

console.log('Successfully reloaded application (/) commands.')
} catch (error) {
console.log(error)
}
}

refreshSlashCommands()

client.on('interactionCreate', async (interaction) => {
if (!interaction.isChatInputCommand()) return
const command = interaction.client.commands.get(interaction.commandName)

if (!command || !command.slash) {
console.log(`No command matching ${interaction.commandName} was found.`)
return
}

try {
await command.execute(client, interaction)
} catch (error) {
console.log(error)
if (interaction.deferred || interaction.replied) {
await interaction.followUp({
content: 'There was an error while executing this command!',
ephemeral: true
})
} else {
await interaction.reply({
content: 'There was an error while executing this command!',
ephemeral: true
})
}
}
})

client.on('messageCreate', async (message) => {
const config = await loadHarmonyConfig()

if (message.author.bot) return
if (!message.content.startsWith(config.defaultPrefix)) return

const commandName = message.content.slice(config.defaultPrefix.length)
const command = client.commands.get(commandName)

if (!command || command.slash) return

try {
await command.execute(client, message)
} catch (error) {
console.log(error)
message.reply('There was an error while executing this command!')
}
})

client.login(process.env.CLIENT_TOKEN)
12 changes: 12 additions & 0 deletions build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
declaration: true,
rollup: {
inlineDependencies: true,
resolve: {
exportConditions: ['production', 'node'] as any
}
},
entries: ['./src/index']
})
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "core",
"version": "1.0.0",
"description": "",
"license": "ISC",
"type": "module",
"scripts": {
"prepack": "unbuild",
"start": "node ./bin/index.mjs",
"lint": "prettier --write ."
},
"types": "./dist/index.d.ts",
"dependencies": {
"c12": "^1.10.0",
"defu": "^6.1.4",
"discord.js": "^14.15.1",
"dotenv": "^16.4.5",
"jiti": "^1.21.0",
"pathe": "^1.1.2",
"unbuild": "^2.0.0"
},
"devDependencies": {
"@types/node": "^20.12.8",
"prettier": "^3.2.5",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
}
}
25 changes: 25 additions & 0 deletions playground/commands/moderations/ban.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { defineCommand } from '../../../src'

export default defineCommand(
{
slash: true,
description: 'Ban a user from the server',
args: [
{
name: 'user',
description: 'The user to kick',
type: 6,
required: true
},
{
name: 'reason',
description: 'The reason for banning the user',
type: 3,
required: false
}
]
},
(_, message) => {
message.reply('Banned user')
}
)
24 changes: 24 additions & 0 deletions playground/commands/moderations/kick.slash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { defineCommand } from '../../../src'

export default defineCommand(
{
description: 'Kick a user from the server',
args: [
{
name: 'user',
description: 'The user to kick',
type: 6,
required: true
},
{
name: 'reason',
description: 'The reason for kicking the user',
type: 3,
required: false
}
]
},
(_, message) => {
message.reply('Kicked user')
}
)
10 changes: 10 additions & 0 deletions playground/commands/utils/ping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineCommand } from '../../../src'

export default defineCommand(
{
description: 'Pong!'
},
(client, message) => {
message.reply('Pong ' + client.ws.ping + 'ms!')
}
)
10 changes: 10 additions & 0 deletions playground/commands/utils/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineCommand } from '../../../src'

export default defineCommand(
{
description: 'Test the bot'
},
() => {
console.log('Tested the bot')
}
)
5 changes: 5 additions & 0 deletions playground/events/ready.once.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineEvent } from '../../src'

export default defineEvent({}, (client) => {
console.log(`Ready! Logged in as ${client.user?.tag}`)
})
3 changes: 3 additions & 0 deletions playground/harmony.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineHarmonyConfig } from '../src'

export default defineHarmonyConfig({})
Loading

0 comments on commit fb73bda

Please sign in to comment.