diff --git a/example/commands/hello.js b/example/commands/hello.js new file mode 100644 index 000000000..b8f4a9f31 --- /dev/null +++ b/example/commands/hello.js @@ -0,0 +1,38 @@ +const {Command, CommandType, MessageActionRow, MessageButton, CustomId} = require('../../dist'); + +new Command({ + name: 'hello', + description: 'Says hello!', + type: [CommandType.SLASH, CommandType.MESSAGE], + run: (ctx) => { + const row = new MessageActionRow().addComponents([ + new MessageButton() + .setCustomId(CustomId('hello', ctx.userId)) + .setLabel('Click me!') + .setStyle('SUCCESS') + ]); + + return ctx.reply({content: `Hello ${ctx.username}!`, components: [row]}); + } +}); + +new class extends Command { + constructor() { + super({ + name: 'hello', + description: 'Says hello!', + type: [CommandType.SLASH, CommandType.MESSAGE], + }); + } + + run(ctx) { + const row = new MessageActionRow().addComponents([ + new MessageButton() + .setCustomId(CustomId('hello', ctx.userId)) + .setLabel('Click me!') + .setStyle('SUCCESS') + ]); + + return ctx.reply({content: `Hello ${ctx.username}!`, components: [row]}); + } +}; diff --git a/example/commands/introduce.js b/example/commands/introduce.js new file mode 100644 index 000000000..f47112990 --- /dev/null +++ b/example/commands/introduce.js @@ -0,0 +1,32 @@ +const {Argument, ArgumentType, Command, CommandType, CustomId, MessageActionRow, MessageButton} = require('../../dist'); +const { MongoDBProvider } = require('../../dist/providers/MongoDBProvider'); + +// You can still use classes, but you will need to put the "new" keyword front of it +new Command({ + name: 'introduce', + description: 'Introduce yourself', + type: [CommandType.SLASH, CommandType.CONTEXT_USER, CommandType.MESSAGE], + arguments: [ + new Argument('name', { + description: 'Your name', + type: ArgumentType.STRING, + required: true, + }) + ], + run: (ctx) => { + const name = ctx.arguments.getUser('user')?.username || ctx.arguments.getString('name'); + + const row = new MessageActionRow().addComponents([ + new MessageButton() + .setCustomId(CustomId('introduce', name, ctx.userId)) + .setLabel('Click me!') + .setStyle('SUCCESS'), + ]); + + return ctx.reply({content: `Hello ${name}! I am ${ctx.client.user.username}.`, components: [row]}); + }, + // Called when an error occurs during the execution of run() + onError: (ctx) => { + return ctx.reply('I dont think I want to introduce myself to you.'); + } +}); diff --git a/example/components/hello.js b/example/components/hello.js new file mode 100644 index 000000000..b5a233307 --- /dev/null +++ b/example/components/hello.js @@ -0,0 +1,22 @@ +const {Component, ComponentType} = require('../../dist'); + +new Component({ + name: 'hello', + type: [ComponentType.BUTTON], + run: (ctx) => { + return ctx.reply(`Hello again ${ctx.username}!`); + } +}); + +new class extends Component { + constructor() { + super({ + name: 'hello', + type: [ComponentType.BUTTON], + }); + } + + run(ctx) { + return ctx.reply(`Hello again ${ctx.username}!`); + } +}; diff --git a/example/components/introduce.js b/example/components/introduce.js new file mode 100644 index 000000000..cc5b33171 --- /dev/null +++ b/example/components/introduce.js @@ -0,0 +1,13 @@ +const {Component, ComponentType} = require('../../dist'); + +// You can still use classes, but you will need to put the "new" keyword front of it +new Component({ + name: 'introduce', + type: [ComponentType.BUTTON], + run: (ctx) => { + ctx.reply(`Hello again ${ctx.arguments[0]}!`); + }, + onError: (ctx) => { + ctx.reply(`I dont want to say hello to you ${ctx.arguments[0]}`); + } +}); diff --git a/example/index.js b/example/index.js new file mode 100644 index 000000000..8188c8364 --- /dev/null +++ b/example/index.js @@ -0,0 +1,32 @@ +// const {GClient} = require('gcommands')! +require('dotenv').config(); +const {GClient, Plugins, Logger} = require('../dist'); +const {Intents} = require('discord.js'); +const path = require('path'); + +// Search for plugins in node_modules (folder names starting with gcommands-plugin- or @gcommands/plugin-) or plugins folder +Plugins.search(__dirname); + +// Set the log level +Logger.setLevel(Logger.TRACE); + +const client = new GClient({ + dirs: [ + path.join(__dirname, 'commands'), + path.join(__dirname, 'components'), + path.join(__dirname, 'listeners') + ], + messagePrefix: '!', + devGuildId: process.env.DEV_SERVER, + intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES], +}); + +// See commands/introduce.js (new syntax) +// See commands/class.js (old class syntax) +// See components/introduce.js (new syntax) +// See listeners/ready.js (new syntax) + +client.on('error', console.log); +client.on('warn', console.log); + +client.login(process.env.token); diff --git a/example/listeners/ready.js b/example/listeners/ready.js new file mode 100644 index 000000000..6060b29cd --- /dev/null +++ b/example/listeners/ready.js @@ -0,0 +1,10 @@ +const {Listener} = require('../../dist'); + +new Listener({ + name: 'ready', + event: 'ready', + once: true, + run: (client) => { + return console.log(`Ready! Initialized with ${client.guilds.cache.size} guilds`); + } +});