Why my bot showing info only in terminal? - javascript

I want to display player count on my Discord server like that:
Desired output
If I use the !players command
const Gamedig = require('gamedig');
Gamedig.query({
type: 'samp',
host: '91.121.87.14',
port: 9180
}).then((state) => {
console.log(state);
}).catch((error) => {
console.log("Server is offline");
});
I run my Discord bot with this code and it's showing me the info but only in the terminal I want like that:
Output example
I just want my bot to display player count like above in the pic

You need to tell your code to post a message in Discord.Try Discord Webhooks.

You use the lib "gamedig" which provides the following function:
node-GameDig is a game server query library, capable of querying for the status of almost any game or voice server.
(https://github.com/sonicsnes/node-gamedig)
Currently, the data read from "gamedig" is only output via the console.
In order to display the data in Discord like on your first screenshot, you have to talk to the Discord API (https://discord.com/developers/docs/intro)

The question is: "Why my bot showing info only in terminal?"
The answer is: Because you asked it only to console.log
I don't know your lib, but it seems it only returns data from the server.
Use discord.js to make a discord bot https://discord.js.org/#/
Hope it helped

Related

How to send a message to another Discord server on a specific channel? [duplicate]

I'm failing to achieve something very simple. I can't send a message to a specific channel. I've browsed trough the documentation and similar threads on stack overflow.
client.channels.get().send()
does NOT work.
It is not a function.
I also do not see it as a method on the Channel class on the official documentation yet every thread I've found so far has told me to use that.
I managed to have the bot reply to a message by listening for a message and then using message.reply() but I don't want that. I want my bot to say something in a specific channel in client.on('ready')
What am I missing?
You didn't provide any code that you already tested so I will give you the code for your ready event that will work!
client.on('ready', client => {
client.channels.get('CHANNEL ID').send('Hello here!');
})
Be careful that your channel id a string.
Let me know if it worked, thank you!
2020 Jun 13 Edit:
A Discord.js update requires the cache member of channels before the get method.
If your modules are legacy the above would still work. My recent solution works changing the send line as follows.
client.channels.cache.get('CHANNEL ID').send('Hello here!')
If you are using TypeScript, you will need to cast the channel in order to use the TextChannel.send(message) method without compiler errors.
import { TextChannel } from 'discord.js';
( client.channels.cache.get('CHANNEL ID') as TextChannel ).send('Hello here!')
for v12 it would be the following:
client.on('messageCreate', message => {
client.channels.cache.get('CHANNEL ID').send('Hello here!');
})
edit: I changed it to log a message.
This will work for the newest version of node.js msg.guild.channels.cache.find(i => i.name === 'announcements').send(annEmbed)
This should work
client.channels.fetch('CHANNEL_ID')
.then(channel=>channel.send('booyah!'))
Here's how I send a message to a specific channel every time a message is deleted. This is helpful if you'd like to send the message without a channel ID.
client.on("messageDelete", (messageDelete) => {
const channel = messageDelete.guild.channels.find(ch => ch.name === 'channel name here');
channel.send(`The message : "${messageDelete.content}" by ${messageDelete.author} was deleted. Their ID is ${messageDelete.author.id}`);
});
Make sure to define it if you're not using messageDelete.

How do I make my command not mention the user?

I just started coding my Discord bot and I made a command that replies to every message that isn’t sent by a bot. When I tried it in DMs it works fine, but when I tried it in my server it would mention the user before the command. In the DM it would say just “test”, but in the server it would say something like “#ExampleUser, test”.
Is there a way I can fix this? Here’s my code:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on("message", (message) => {
if (message.author.bot) return;
return message.reply("test")
});
Instead of message.reply('test') use message.channel.send('test') that sends a message to the channel the original message was sent to.
Solution
use message.channel.send('string'); instead of .reply()
It's very easy :)
Instead of saying message.reply('message') you just have to say message.channel.send('message')

How to send a startup message when my bot gets online in all servers it is in discord. Js?

So I'm basically trying to send my bot update log of commands I have removed and added along with its new version info and data as soon as I start it in my terminal or gets online in all server it's available in currently I'm trying this code :-
Client.on{message.channel.send('Bot Name:- My bot \n Bot Verison :- 1.0.0 \n Owner :- Rega!
You need to add a listener from the client to the ready event. Here's your code, corrected and reformated.
// The callback gets called when the bot is ready
client.on('ready', () => {
// Get the channel from its ID
const logChannel = client.channels.cache.get('channel-id')
// Send the message
logChannel.send('The bot is up!')
})
You naturally need to replace channel-id with the ID of your channel. You can find more about it in this Discord Support article.

How do I change the Embeds Description from a different command in Discord.js

So I have been coding my own Discord bot for a while now and I want to make a command that changes an embed from another command. I have a command handler.
When I do -autostatus my Discord bot will post an embed with latency, API latency, uptime, and status information on whether the bot is functioning fine.
I have got the "functioning fine" and uptime inside the .setDescription so then I can edit it from another command. But when I do -botoff, I get the following error:
embed.setDescription("Uptime: Offline Bot Status: Offline")
TypeError: Cannot read property of 'setDescription' of undefined
Here is my -botoff command:
const Discord = require("discord.js")
const { embed } = require("./autostatus.js")
exports.run = (client, message, args) => {
if(message.author.id != "276249983664128001") return;
embed.setDescription("Uptime: Offline Bot Status: Offline")
}
My autostatus.js can be found here: https://hastebin.com/pezinewiqi.js
Thanks so much if you can help!
You are getting this error because const { embed } = require("./autostatus.js")
is undefined. This is because you are not exporting embed in autostatus.js.
I checked your hastebin and found
let embed = new Discord.MessageEmbed()
.setTitle("My Status!")
.setDescription(`**Uptime:** \n \`${uptime}\` \n **Bot Status:** \`✅ Operational\``)
.addField("Latency:", `\`${ping}\``)
.addField("API Latency:", `\`${Math.round(client.ws.ping)}\``)
.setColor(client.config.color)
.setFooter(`${client.config.footer} | Last updated`)
.setTimestamp()
Here by using let you are just creating a local variable. This can't be required from other files.
You need to save the embed message ID in a global variable of your bot and then, in your other command, fetch the message by its ID and edit the content of the fetched message.

Start conversation with Wit.ai chat bot in node.js

I have created a story on wit.ai using the quickstart guide.
Now I want to make a conversation with my chat bot using node-wit in node.js.
I guess I should use https://github.com/wit-ai/node-wit#runactions to run the messages, but I'm not sure how to start a conversation that never ends. I need to send a message and then get the response from the chat bot until I break the conversation.
I have looked through the wit.ai examples, but I cannot find any example of how to start a simple conversation in node.js.
I use socket.io to transmit the messages between client and server, and I have tried to solve my problem with
let sessions = {};
const sessionId = new Date().toISOString();
sessions[sessionId] = { context: {} };
io.on('connection', function (socket) {
socket.on('new message', function (message) {
client.runActions(
sessionId,
message,
sessions[sessionId].context
).then((context) => {
console.log(context);
sessions[sessionId].context = context;
}).catch((err) => {
console.error('Error: ', err.stack || err);
});
});
});
and it seems to almost work. I can chat with my bot, but it messes up the stories by sometimes answering multiple times from different stories. I guess I should probably end the stories somehow?
You should try with this link
https://github.com/wit-ai/node-wit/blob/master/examples/quickstart.js
Just clone/download the whole node-wit module from git or npm-install.
Then just run the command node quickstart.js "wit-token".
wit-token == wit-app-token
it will work .
Have you checked this Facebook Messenger integration example. The quickstart.js includes an interactive mode this is why it may be confusing.
Look at the messenger.js example on how to use runActions and send messages back to Messenger.
I was successful in doing this, although I'm still working on stories.

Categories