zkyc
Version:
Discord command handler.
252 lines (219 loc) • 8.2 kB
JavaScript
function getNewCommandTemplate(name, descrirption) {
return `const Command = require("../Structures/Command.js");
module.exports = new Command({
name: "${name}",
description: "${descrirption}",
async run(message, args, client) {
// Put your code here.
}
});`;
};
exports.getNewCommandTemplate = getNewCommandTemplate;
function getNewEventTemplate(name) {
return `const Event = require("../Structures/Command.js");
module.exports = new Event("${name}", (client) => {
// Put your code here.
});`;
};
exports.getNewEventTemplate = getNewEventTemplate;
function getIndexTemplate() {
return `const Client = require("./Structures/Client.js");
const config = require("./zkyc.json");
const client = new Client();
client.start();
client.login(config.token);`;
};
exports.getIndexTemplate = getIndexTemplate;
function getConfigTemplate(projectName, projectTemplate, projectToken, projectPrefix) {
return `{
"name": "${projectName}",
"template": ${projectTemplate},
"token": "${projectToken}",
"prefix": "${projectPrefix}"
}`;
};
exports.getConfigTemplate = getConfigTemplate;
function getPerServerPrefixConfigTemplate(projectName, projectTemplate, projectToken, projectDefaultPrefix) {
return `{
"name": "${projectName}",
"template": ${projectTemplate},
"token": "${projectToken}",
"defaultPrefix": "${projectDefaultPrefix}"
}`;
};
exports.getPerServerPrefixConfigTemplate = getPerServerPrefixConfigTemplate;
function getClientTemplate() {
return `const DJS = require("discord.js");
const fs = require("fs");
const Command = require("./Command.js");
const Event = require("./Event.js");
const intents = new DJS.Intents(32767);
class Client extends DJS.Client {
constructor() {
super({ intents });
/**
* @type {DJS.Collection<string, Command>}
*/
this.command = new DJS.Collection();
}
start() {
fs.readdirSync("./Commands").filter(file => file.endsWith(".js")).forEach(file => {
/**
* @type {Command}
*/
const command = require("../Commands/" + file);
console.log("Command " + command.name + " loaded");
this.command.set(command.name, command);
});
fs.readdirSync("./Events").filter(file => file.endsWith(".js")).forEach(file => {
/**
* @type {Event}
*/
const event = require("../Events/" + file);
console.log("Event " + event.event + " loaded");
this.on(event.event, event.run.bind(null, this));
});
}
};
module.exports = Client;`;
};
exports.getClientTemplate = getClientTemplate;
function getCommandTemplate() {
return `const DJS = require("discord.js");
const Client = require("./Client.js");
/**
*
* @param {DJS.Message | DJS.Interaction} message
* @param {string[]} args
* @param {Client} client
*/
function RunFunction(message, args, client) { }
class Command {
/**
* @typedef {{name: string, description: string, run: RunFunction}} CommandOptions
* @param {CommandOptions} options
*/
constructor(options) {
this.name = options.name;
this.description = options.description;
this.run = options.run;
}
}
module.exports = Command;`;
};
exports.getCommandTemplate = getCommandTemplate;
function getEventTemplate() {
return `const DJS = require("discord.js");
const Client = require("./Client.js");
/**
* @template {keyof DJS.ClientEvents} K
* @param {Client} client
* @param {DJS.ClientEvents[K]} eventArgs
*/
function RunFunction(client, guild, ...eventArgs) { }
/**
* @template {keyof DJS.ClientEvents} K
*/
class Event {
/**
*
* @param {K} event
* @param {RunFunction<K>} runFunction
*/
constructor(event, runFunction) {
this.event = event;
this.run = runFunction;
}
}
module.exports = Event;`;
};
exports.getEventTemplate = getEventTemplate;
function getMessageCreateTemplate() {
return `const Event = require("../Structures/Event.js");
const DJS = require("discord.js");
const config = require("../zkyc.json");
const prefix = config.prefix;
module.exports = new Event("messageCreate", (client, message) => {
if (message.guild.me.permissionsIn(message.channel).has(DJS.Permissions.FLAGS.SEND_MESSAGES)) {
if (!message.content.startsWith(prefix)) return;
const args = message.content.substring(prefix.length).split(/ +/);
const command = client.command.find(cmd => cmd.name == args[0]);
if (!command) return message.reply(args[0] + " is not a valid command!");
command.run(message, args, client);
} else {
console.log("Don't have permissions to send messages in Guild: " + message.guild.name + ". Channel: " + message.channel.name);
}
});`;
};
exports.getMessageCreateTemplate = getMessageCreateTemplate;
function getReadyEventTemplate() {
return `const Event = require("../Structures/Event.js");
module.exports = new Event("ready", (client) => {
console.log("Bot is ready!");
});`;
};
exports.getReadyEventTemplate = getReadyEventTemplate;
function getMessageCreateTemplatePrefix() {
return `const Event = require("../Structures/Event.js");
const DJS = require("discord.js");
const data = require("../Data/serverdata.json");
module.exports = new Event("messageCreate", (client, message) => {
const prefix = data[client.guild.id].prefix;
if (message.guild.me.permissionsIn(message.channel).has(DJS.Permissions.FLAGS.SEND_MESSAGES)) {
if (!message.content.startsWith(prefix)) return;
const args = message.content.toLowerCase().substring(prefix.length).split(/ +/);
const command = client.command.find(cmd => cmd.name == args[0]);
if (!command) return message.reply(args[0] + " is not a valid command!");
command.run(message, args, client);
} else {
console.log("Don't have permissions to send messages in Guild: " + message.guild.name + ". Channel: " + message.channel.name);
}
});`;
};
exports.getMessageCreateTemplatePrefix = getMessageCreateTemplatePrefix;
function getJoinServerTemplate() {
return `const Event = require("../Structures/Event.js");
const fs = require("fs");
const DJS = require("discord.js");
const config = require("../zkyc.json");
const data = require("../Data/serverData.json");
module.exports = new Event("guildCreate", (client, guild) => {
console.log(guild.name + " - added the bot to their server.");
if (data[guild.id] == undefined) {
data[guild.id] = {
prefix: config.defaultPrefix
};
fs.writeFile("./Data/serverData.json", JSON.stringify(data, null, 4), err => {
if (err) throw err;
});
};
});`;
};
exports.getJoinServerTemplate = getJoinServerTemplate;
function getServerDataTemplate() {
return `{}`;
};
exports.getServerDataTemplate = getServerDataTemplate;
function getPrefixCommandTemplate() {
return `const Command = require("../Structures/Command.js");
const fs = require("fs");
const DJS = require("discord.js");
const data = require("../Data/serverData.json");
module.exports = new Command({
name: "prefix",
description: "Sets the prefix for the server.",
async run(message, args, client) {
const newPrefix = args[1];
if (!message.member.permissions.has(DJS.Permissions.FLAGS.ADMINISTRATOR)) {
message.reply("You don't have permission to execute this command.");
} else {
data[message.guild.id].prefix = newPrefix;
fs.writeFile("./Data/serverData.json", JSON.stringify(data, null, 4), err => {
if (err) throw err;
message.reply("Changed the prefix to " + args[1]);
});
}
}
}); `;
};
exports.getPrefixCommandTemplate = getPrefixCommandTemplate;