Skip to content

Commit

Permalink
fix: revent examples
Browse files Browse the repository at this point in the history
  • Loading branch information
xhyrom committed Jan 24, 2022
1 parent ea27aba commit 763e044
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 0 deletions.
38 changes: 38 additions & 0 deletions example/commands/hello.js
Original file line number Diff line number Diff line change
@@ -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]});
}
};
32 changes: 32 additions & 0 deletions example/commands/introduce.js
Original file line number Diff line number Diff line change
@@ -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.');
}
});
22 changes: 22 additions & 0 deletions example/components/hello.js
Original file line number Diff line number Diff line change
@@ -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}!`);
}
};
13 changes: 13 additions & 0 deletions example/components/introduce.js
Original file line number Diff line number Diff line change
@@ -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]}`);
}
});
32 changes: 32 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -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);
10 changes: 10 additions & 0 deletions example/listeners/ready.js
Original file line number Diff line number Diff line change
@@ -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`);
}
});

0 comments on commit 763e044

Please sign in to comment.