New command handler and command files. I will add more examples and documentation. please wait - Update app.js, bootscreen.js, and 5 more files...

This commit is contained in:
2020-10-12 11:53:15 +02:00
parent b3b9f71976
commit 8950e3271b
7 changed files with 93 additions and 94 deletions

View File

@@ -5,30 +5,38 @@ const zoizbot = new Discord.Client(); // discord client is now made zoizbot. wat
var fs = require('fs');
zoizbot.commands = new Discord.Collection();
console.log("==============================================");
console.log(`starting zoizbot...`);
eval(fs.readFileSync('./bootscreen.js') + ''); // please delete this, using readFileSync 1 time was bad enough. But I didnt want ascii art in my main file ;)
console.log(`loading configuration file...`)
const config = require("./conf.json"); // de conf ophalen die we net gemaakt hadden / picking up the config file we made
console.log(`Ready!`) //normally if you would make it properly you would catch any errors when loading the config
console.log(`configuration loaded!`) // should make an catch error to debug conf file but whatever
console.log("==============================================");
console.log(`loading the commandhandler...`)
zoizbot.on("ready", () => {
console.log(`Zoizbot has started, and is serving ${zoizbot.users.cache.size} users, in ${zoizbot.guilds.cache.size} guilds.`); // you can display anything you want here, all the functions are available in de documentation from discord.js
eval(fs.readFileSync('./commandhandler.js') + '');
zoizbot.user.setActivity(`Ask me something with >`); // change this to whatever you want. check out https://discord.js.org/#/docs/main/12.3.1/general/welcome
console.log(`commandhandler loaded!`)
console.log("==============================================");
console.log(`loading command-files...`)
fs.readdir(`./commands/`, (err, files) => {
if (err) return console.error(err);
files.forEach(file => {
if (!file.endsWith(".js")) return; // only load js files. no garbage collection
let props = require(`./commands/${file}`); // get the command name from the file.js
let commandName = file.split(".")[0];
console.log(`attempting to load command ${commandName}`); // adding everything to the collection
zoizbot.commands.set(commandName, props);
});
console.log(`all commands loaded!`);
console.log("==============================================");
});
zoizbot.on("guildJoin", guild => {
console.log(`I have joined ${guild.name}, I will be serving ${guild.memberCount} members!`); // Join server message in the console
message.channel.send(`Hello ${guild.name} thank you for having me!`);
});
zoizbot.on("guildLeave", guild => {
console.log(`Adios! i will be leaving: ${guild.name} It was fun while it lasted!`);
});
eval(fs.readFileSync('./commands/examples.js') + ''); //dirty but working trick to keep app main clean...
zoizbot.login(config.token); //reading the "token" from the const config = conf.json btw the one you see is invalid now

15
zoizbot/bootscreen.js Normal file
View File

@@ -0,0 +1,15 @@
// bad practice please remove this file and the fileReadSync function
console.log("==============================================");
console.log(" ,--, ");
console.log(" ,----, ,---. ,--.'| ,----, ");
console.log(" .' .`| ' ,'\ | |, .' .`| ");
console.log(" .' .' .' / / |`--'_ .' .' .' ");
console.log(",---, ' ./ . ; ,. :,' ,'| ,---, ' ./ ");
console.log("; | .' / ' | |: :' | | ; | .' / ");
console.log("`---' / ;--,' | .; :| | : `---' / ;--, ");
console.log(" / / / .`|| : |' : |__ / / / .`| ");
console.log("./__; .' \ \ / | | '.'|./__; .' ");
console.log("; | .' `----' ; : ;; | .' ");
console.log("`---' | , / `---' ");
console.log(" ---`-' ");
console.log("==============================================");

25
zoizbot/commandhandler.js Normal file
View File

@@ -0,0 +1,25 @@
zoizbot.on("ready", () => {
zoizbot.user.setActivity(config.activity); // change this to whatever you want (in conf.json).
console.log(`Zoizbot has started, and is serving ${zoizbot.users.cache.size} users, in ${zoizbot.guilds.cache.size} guilds.`); // you can display anything you want here, all the functions are available in de documentation from discord.js
console.log("==================LISTENING===================");
});
zoizbot.on("guildJoin", guild => {
console.log(`I have joined ${guild.name}, I will be serving ${guild.memberCount} members!`); // Join server message in the console
message.channel.send(`Hello ${guild.name} thank you for having me!`);
});
zoizbot.on("guildLeave", guild => {
console.log(`Adios! i will be leaving: ${guild.name}, it was fun while it lasted!`); //letting you know he be headin out
});
zoizbot.on("message", async message => {
if (message.author.bot) return; //ignore bots
if (message.content.indexOf(config.prefix) !== 0) return; // dont waste time listening to non prefix commands
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase(); // slice and dice && lowercase
const cmd = zoizbot.commands.get(command);
if (!cmd) return; //if the command doesnt exist gracefully return
cmd.run(zoizbot, message, args); // and fire!
});

View File

@@ -1,78 +0,0 @@
zoizbot.on("message", async message => {
if (message.author.bot) return; // dont listen to other bots
if (!message.content.startsWith(config.prefix)) return; // dont waste time listening to non prefix messages
const args = message.content.slice(config.prefix.length).trim().split(/ +/g); // cut the arguments away from the command.
const command = args.shift().toLowerCase();
//ping pong, example from documentation
if (command === "ping") {
const m = await message.channel.send("Ping?");
m.edit(`Pong! Latency is ${m.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(zoizbot.ws.ping)}ms`);
}
//simple kick command
if (command === "kick") {
//if (!message.member.roles.cache.some(r => ["Administrator", "Moderator"].includes(r.name))) checks the roles for permissions
if (!message.member.hasPermission("KICK_MEMBERS"))
return message.reply("Sorry, you don't have permissions to use this!");
let member = message.guild.member(message.mentions.users.first()) || message.mentions.members.first(args[0])
if (!member)
return message.reply("Please mention a valid member of this server");
if (!member.kickable)
return message.reply("I cannot kick this user! Do they have a higher role? Do I have kick permissions?");
let KickReason = args.slice(1).join(' '); // slicing the arguments
if (!KickReason) KickReason = "No reason provided";
await member.kick(KickReason)
.catch(error => message.reply(`Sorry ${message.author} I couldn't kick because of : ${error}`)); // now an adios moment
message.reply(`${member.user.tag} has been kicked by ${message.author.tag} because: ${KickReason}`);
}
//simple ban command
if (command === "ban") {
if (!message.member.hasPermission("BAN_MEMBERS"))
return message.reply("Sorry, you don't have permissions to use this!")
let member = message.guild.member(message.mentions.users.first()) || message.mentions.members.first(args[0])
if (!member)
return message.reply("Please mention a valid member of this server")
if (member.hasPermission("BAN_MEMBERS"))
return message.reply("I cannot ban this user! Invalid Permissions")
let banReason = args.join(" ").slice(22);
if (!banReason) {
banReason = "No reason provided"
}
member.ban({ reason: banReason })
return message.reply(`${member.user.tag} has been banned by ${message.author.tag} because: ${banReason}`)
}
//simple purge example command
if (command === "purge") {
const deleteCount = parseInt(args[0], 10);
if (!deleteCount || deleteCount < 2 || deleteCount > 100)
return message.reply("Please provide a number between 2 and 100 for the number of messages to delete");
const fetched = await message.channel.messages.fetch({ limit: deleteCount });
message.channel.bulkDelete(fetched)
.catch(error => message.reply(`Couldn't delete messages because of: ${error}`));
}
else return
});

12
zoizbot/commands/hello.js Normal file
View File

@@ -0,0 +1,12 @@
/*
* Message example. When you >hello it will say hello to you
* Added the reply so you dont have to get the member declaration but let the "discord.js" handle it
* Check out readme for more information
* catching errors is indeed the preferred way of debugging your commands. Nodejs is terrible in debugging
* make sure you use a decent IDE
*
*/
exports.run = (zoizbot, message, args) => {
message.channel.send("Hello!").catch(console.error);
message.reply("Thank you for waking me up, i nearly fell asleep :)");
}

6
zoizbot/commands/ping.js Normal file
View File

@@ -0,0 +1,6 @@
// ping pong example from the documentation with async await
// make all the calculations you want and change the message
exports.run = async(zoizbot, message, args) => {
const m = await message.channel.send("Ping?");
m.edit(`Pong! Latency is ${m.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(zoizbot.ws.ping)}ms`);
}

View File

@@ -36,14 +36,25 @@
<AdditionalFiles Include="conf.json">
<SubType>Code</SubType>
</AdditionalFiles>
<Content Include="commands\examples.js">
<Content Include="bootscreen.js">
<SubType>Code</SubType>
</Content>
<Content Include="commandhandler.js">
<SubType>Code</SubType>
</Content>
<Content Include="commands\hello.js">
<SubType>Code</SubType>
</Content>
<Content Include="commands\ping.js">
<SubType>Code</SubType>
</Content>
<Content Include="Legacy Commands\examples.js" />
<Content Include="package.json" />
<Content Include="README.md" />
</ItemGroup>
<ItemGroup>
<Folder Include="commands\" />
<Folder Include="Legacy Commands\" />
</ItemGroup>
<!-- Do not delete the following Import Project. While this appears to do nothing it is a marker for setting TypeScript properties before our import that depends on them. -->
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets" Condition="False" />