I am learning to write Discord bots and the basics of working with the Discord.js library in general. I wrote the following code which turns on, but does not execute any commands.
Index.js (main file):
const { Client, Collection, GatewayIntentBits } = require('discord.js');
const fs = require('fs');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const config = require('./data.json');
client.commands = new Collection();
fs.readdir('./commands', (err, files) => {
if (err) console.log(err);
let jsfile = files.filter(f => f.split('.').pop() === 'js');
if (jsfile.length <= 0) return console.log('No commands found!');
console.log(`Uploaded ${jsfile.length} comands`);
jsfile.forEach((f, i) => {
let props = require(`./commands/${f}`);
client.commands.set(props.help.name, props);
});
});
client.on('ready', () => {
console.log(`Bot ${client.user.username} was started`);
});
client.on('messageCreate', async message => {
let prefix = config.prefix;
if (!message.content.startsWith(prefix) || message.author.bot) return;
let messageArray = message.content.split(' ');
let command = messageArray[0];
let args = messageArray.slice(1);
let command_file = client.commands.get(command.slice(prefix.length));
if (command_file) command_file.run(client, message, args, prefix);
});
client.login(config.token);
Commands and events: say.js The bot will delete the user's command and write what is actually written after the command itself! Example: !say Hello *Message deleted * And the bot writes - Hello!
const { EmbedBuilder } = require('discord.js');
const fs = require("fs");
module.exports.run = async (client,message,args) => {
let sms = args.join(" ");
message.channel.bulkDelete(1);
message.channel.send(sms);
console.log(`User ${message.author.username} sent via say: ${args.join(" ")}`);
const adminerr2 = new EmbedBuilder()
.setColor('#fc5184')
.setTitle(`${message.author.username}, you have no rights to this command!`)
.setAuthor(message.guild.name)
.setFooter('Amy<3 © 2023')
message.channel.send(adminerr2)
};
module.exports.help = {
name: "say"
};
hi.js
const { EmbedBuilder } = require('discord.js');
const fs = require("fs");
module.exports.run = async (client,message,args,prefix) => {
const exampleEmbed = new EmbedBuilder()
.setColor('#43e2f7')
.setTitle('Hi :>')
.setAuthor(message.guild.name)
.setDescription(':^We love you!^:')
.setTimestamp()
.setFooter('Amy<3 © 2023');
message.channel.send(exampleEmbed);
};
module.exports.help = {
name: "hi"
};
ping.js
const Discord = module.require("discord.js");
const fs = require("fs");
module.exports.run = async (client,message,args,prefix) => {
const ping = new Date(message.createdTimestamp);
const timeTaken = Date.now() - message.createdTimestamp;
message.channel.send(`Ping: ${timeTaken}ms`);
};
module.exports.help = {
name: "ping"
};
wasted.js After entering the command, the bot copies the avatar of the user who wrote the command, adjusts the effects and returns it in the style of the death screen from GTA.
const { AttachmentBuilder,EmbedBuilder } = require('discord.js');
const fs = require("fs");
module.exports.run = async (client,message,args) => {
if (!message.mentions.users.size) {
let link = `https://some-random-api.ml/canvas/wasted/?avatar=${message.author.avatarURL({ format: 'png'})}`
const attachmentt = new AttachmentBuilder(link, 'triggered.gif');
const embed = new EmbedBuilder()
.setTitle(`${message.author.username} Wasted!`)
.attachFiles(attachmentt)
.setImage('attachment://triggered.gif')
return message.channel.send(embed);
}
const WastedList = message.mentions.users.map(user => {
let link = `https://some-random-api.ml/canvas/wasted/?avatar=${user.avatarURL({ format: 'png'})}`
const attachmentt = new AttachmentBuilder(link, 'triggered.gif');
const embed = new EmbedBuilder()
.setTitle(`${user.username} Wasted!`)
.attachFiles(attachmentt)
.setImage('attachment://triggered.gif')
return embed
});
message.channel.send(WastedList);
return
};
module.exports.help = {
name: "wasted"
};