Resend an image automatically - node.js

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())
})

Related

Discord.js send a medium post to a discord channel

I would like to try send medium's post links into discord channel. However i didn't find much resources online. I found some SDK library but that really seems to be outdated https://www.npmjs.com/package/medium-sdk. It is possible to use Medium API somehow to send medium post from certain "user" as a medium post website link to the defined discord channel?
Not sure if someone ever did this. I only saw a posibility to create a medium post when a discord message is sent to the channel somehow. But that's not what I'm looking for.
You can use this package: feed-watcher, and set the feed to https://medium.com/feed/#the_writer_name.
This way, you can get medium posts from the feed-watcher and then send link from the medium post to the discord channel. (Feed is updating approx each 15-20 minutes.) So there might be a delay once medium post is posted to receive data from feed to send link to the channel.
'use strict';
//define your medium link in the feed
var Watcher = require('feed-watcher'),
feed = "https://yourmedium.medium.com/feed?unit=second&interval=5",
interval = 10 // seconds
var watcher = new Watcher(feed, interval)
// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
const config = require("./configtest.json");
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.on('ready', () => {
watcher.on('new entries', function (entries) {
entries.forEach(function (entry) {
const stringifylink = JSON.stringify(entry)
const link = JSON.parse(stringifylink)
console.log('FEED Call response:', link);
console.log('Link:', link.link)
//send new medium post link to the defined discord channel.
client.guilds.cache.forEach(guild => {
let channel = guild.channels.cache.get('ChannelID')
channel.send('Check out our new medium post! ' + link.link)
})
})
})
watcher
.start()
.then(function (entries) {
console.log(entries)
})
.catch(function(error) {
console.error(error)
})
})
// Login to Discord with your client's token
client.login(config.token);

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.

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.

Discord Bot Mentioning

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>

Resources