new command handler, etc

This commit is contained in:
loopdelux 2024-02-20 19:05:02 -05:00
parent f9ef87c1e6
commit 8520bc8628
6 changed files with 81 additions and 3 deletions

10
LICENSE Normal file
View file

@ -0,0 +1,10 @@
This software is under the Revised BSD License
Copyright 2024 Loopdelux
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

4
README
View file

@ -4,4 +4,8 @@ Dependencies:
o Typescript
o Concordia
Credits:
o Jess
o Discordjs.guide
P/S: I'm a beginner at JS/TS; please excuse me for any poor decisions. Feedback is appreciated.

View file

@ -2,6 +2,7 @@
/* Make sure to rename this to config.ts */
export default
{
prefix: "X",
token: "X",
owner: "X"
}

View file

@ -11,6 +11,7 @@
"license": "ISC",
"devDependencies": {
"@types/node": "^20.11.19",
"@types/ws": "^8.5.10",
"typescript": "^5.3.3"
},
"dependencies": {

11
src/commands/hi.ts Normal file
View file

@ -0,0 +1,11 @@
import { Message } from "concordia";
export default
{
name: 'goodnight',
description: 'i lost hours of sleep to ts',
execute(msg: Message, args: string[])
{
msg.reply('good night.');
}
}

View file

@ -1,14 +1,65 @@
/* Starting point of the entire program */
import cfg from "../config";
import { ActivityType, Client } from "concordia";
const client = new Client<true>();
import { ActivityType, Client, Collection, Message } from "concordia";
import fs from "node:fs"
function onReady()
class CommandClient extends Client
{
commands: Collection<string, any>;
constructor()
{
super();
this.commands = new Collection<string, any>();
}
}
const client = new CommandClient();
/* Command Handling */
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles)
{
const command = require(`./commands/${file}`).default;
// set new item in collection with key as command name & value as exported module
client.commands.set(command.name, command);
console.log(command);
}
/* Bot start-up & continued behaviour */
function onReady(): void
{
client.user!.setPresence
({
activity: { name: 'with my balls.'},
status: 'idle'
});
console.log("Logged in!");
}
function onMessage(msg: Message): void
{
if ( msg.author.bot || !msg.content.startsWith(cfg.prefix) )
return;
const args: string[] = msg.content.slice(cfg.prefix.length).trim().split(/ +/);
const command: string = args.shift()!.toLowerCase();
if (!client.commands.has(command)) return;
try
{
client.commands.get(command).execute(msg, args);
}
catch (error)
{
console.error(error);
msg.reply('There was an error...');
}
}
client.on("ready", onReady);
client.on("message", onMessage);
client.login(cfg.token);