Discord Bot Mentioning - bots

I am trying to make my bot send a ping to me when someone types !owner, however, the bot only sends my userID or name unlinked. How do I make it into a ping? I am using NodeJS for this bot.
const Discord = require('Discord.js');
const bot = new Discord.Client();
bot.on('message', (message) => {
if(message.content == '!owner') {
message.channel.send('Hi #"userID" is the owner, do you need help');
}
});
bot.login('BotToken')

Replace #"userID" with <#your_user_id_here>.
For example: <#123456789123456789>

Related

My Discord bot is messaging continuously even though I programmed it to do it once

I was creating a discord bot and was testing it. When I say Hi or Yo it will msg me an emoji.
It is working fine but, the problem is that it is msg the emoji continuously.
Here is my code:
console.log('Beep Beep! 🤖');
const Discord = require('discord.js');
const client = new Discord.Client(); // Client is the object that connects with the discord API itself to run the bot
client.login('API');
client.on('ready', readyDiscord);
function readyDiscord(){
console.log('👍');
}
client.on('message', gotMessage);
function gotMessage(msg){
if (msg.channel.id == '849147593392914453' && msg.content === 'Hi' || 'Yo'){
// msg.reply('🤟🤟');
msg.channel.send('🤟🤟');
}
}
You don't have to follow mine since it seems the answer is already here, but here is what I would do.
Also, don't show your token to anyone. The token is your key to your bot, so I'd recommend resetting your bot's token in the Discord Developer Portal.
Anyways, here is what I would do:
const Discord = require('discord.js');
const client = new Discord.Client();
client.login('TOKEN_HERE');
client.once('ready', () => {
console.log('I am ready to help others! 👍');
});
const channelID = '849147593392914453';
client.on('message', message => {
if (message.channel.id == channelID && message.content.toLowerCase() === 'hi' || 'yo' || 'hey'){
message.reply('Hey! 🤟🤟');
}
});
Seems like you added the API token to this post. API token should be kept private at all times.
Second of all, you can use a closure if you're trying to send your message only once. I tested the attached snippet locally and it works fine. The message will be sent only once.
function gotMessage(msg){
let isReplied = false;
function reply() {
if (!isReplied && (msg.content === 'Hi' || 'Yo')){
console.log('🤟🤟');
isReplied = true
}
}
return reply
}

How do I DM a user when they join the server?

And also make an embed which you have to react to with an emoji to get a verified role in the server
You can detect user joining by an event:
const { Client } = require('discord');
const client = new Client();
client.on('guildMemberAdd', function(member) => {
member.send('Welcome to my server!');
});
client.login('<token>');
Then, you can fetch a channel in the server and send the embed there:
const channel = client.channels.cache.get('<channel-id>');
channel.send(embed)
.then(message => {
//reaction collector
});
For the reaction collector, please refer to the djs guide.

Discord bot is not responding to commands

I started to make a bot for my Discord server, but I'm completely new to it (I have programming skills, but in web development). I made an application on the Discord developer portal, I made a folder on my PC, I created a package.json file, a main.js file, installed node.js, installed discord.js, I deployed my bot on a test server, etc. (not in this order but whatever).
Then, following a tutorial from a site, I made this in the index.js file:
const Discord = require('discord.js');
const client = new Discord.Client();
client.once('ready', () => {
console.log('Ready!');
});
client.on('message', message => {
if (message.content === '!ping') {
message.channel.send('Pong.');
}
});
client.login(' I PUTTED MY TOCKEN HERE ');
When I put the command !ping on the test server I created, the bot remains offline and I don't receive Pong...
Can you please help me, please?
If the bot does not turn on, that means you did not correctly login or start the bot. Try to define token like const token = "BOT TOKEN HERE", then put client.login(token) instead of what you have.
If that does not help, also make sure that you did node . in your terminal, which will start the bot.
So, your whole code should look something like this:
const Discord = require('discord.js');
const client = new Discord.Client();
const token = "bot token here";
client.on('ready', () => {
console.log('Ready!');
});
client.on('message', message => {
if (message.content === '!ping') {
message.channel.send('Pong.');
}
});
client.login(token);
Make sure that your bot is allowed to recieve messages (discord.com/developers > your bot > bot > the two intent-switches).
Then add this into the brackets of new Discord.Client();
{ partials: ['MESSAGE', 'CHANNEL', 'REACTION', 'USER', 'GUILD_MEMBER']} (you can remove some of those parameters but message will be needed).
Sorry for bad English...

Why dialogflow bot stop responding in telegram if i create a telegram bot object with node-telegram-bot-api?

I’ve build a dialogflow chatbot with telegram integration and i need to take paths of images sent by the user in telegram chat. As far as i know dialogflow bot doesn’t listen for images, so i use a telegram bot object for polling messages to get the images, but in this way the dialogflow bot stop responding, even after the polling of the telegram bot is stopped. There is some conflict between the two bot. The only way to “resuscitate” the dialogflow bot is to manually restarting the telegram integration in the dialogflow UI.
There is a way to solve conflict between the two bot so the dialogflow bot continues responding after the telegram bot got the image?
Here it is the code i wrote:
const TG = require('node-telegram-bot-api');
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
function takeImagePath() {
agent.add(`send an image/photo`); // work
const token = 'my telegram bot token';
let telegramBot = new TG(token, {polling: true});
agent.add(`tg bot created`); // work
telegramBot.on('message', async function (msg) {
const chatId = msg.chat.id;
agent.add('bot dialogflow in the listener'); // don't work
if (msg.photo) {
let ImgID = msg.photo[msg.photo.length - 1].file_id;
let imgPath = "";
if (ImgID) {
telegramBot.getFile(ImgID).then((fileObject) => {
imgPath = fileObject.file_path;
}).then(() => telegramBot.sendMessage(chatId, 'image taken')) //work
.catch(error => console.log("error: " + error.message));
}
}
else { await telegramBot.sendMessage(chatId, "it's not an image, telegram bot shutting down"); //work
await telegramBot.stopPolling();
agent.add("bot dialogflow active"); // don't work
}
});
}
let intentMap = new Map();
intentMap.set('Image intent', takeImagePath);
agent.handleRequest(intentMap);
});
Solved. The problem is that webhook and polling are two mutually exclusive method of getting messages. So whereas dialogflow-telegram bot use webhook, the telegram bot object that i created use polling
let telegramBot = new TG(token, {polling: true});
which automatically delete the webhook.
To solve this problem in necessary to set again the webhook after stopping the polling: await bot.stopPolling(); bot.setWebHook("your webhook url").then(r => console.log("webhook response: "+r)).catch(err => console.log("webhook error: "+err.message));
You can find here the webhook url which your dialogflow-telegram bot is using:
https://api.telegram.org/botYourTelegramBotToken/getWebhookInfo
Hope it helps someone.

Resend an image automatically

I would like to make sure that if I send a picture my bot will send it back immediately afterwards in the same channel. Could you help me?
Thank you very much in advance for the help you will give me.
v13
const {Client} = require('discord.js')
const client = new Client()
client.on('messageCreate', ({attachments, author, channel}) => {
// don't reply to bots
if (author.bot) return
// sends the first attachment if there are any
if (attachments.size) channel.send({files: [attachments.first()]})
})
v12
const {Client} = require('discord.js')
const client = new Client()
client.on('message', ({attachments, author, channel}) => {
// don't reply to bots
if (author.bot) return
// sends the first attachment if there are any
if (attachments.size) channel.send(attachments.first())
})

Resources