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.
Related
I have this cloud function using node.js that listen every time a child is added on a specific node, then it sends a notification to the users. However when I added something on the database, it does not send anything. I am working on android studio java. Should I connect the function to the android studio, if it will only listen on the database and then send FCM messages on the device tokens.
also how to do debugging on this, I am using VS code.
This is my code:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.listen = functions.database.ref("/Emergencies/{pushId}")
.onCreate(async (change, context) => {
change.after.val();
context.params.pushId;
// Get the list of device notification tokens. Note: There are more than 1 users in here
const getDeviceTokensPromise = admin.database()
.ref("/Registered Admins/{uid}/Token").once("value");
// The snapshot to the user's tokens.
let tokensSnapshot;
// The array containing all the user's tokens.
let tokens;
const results = await Promise.all([getDeviceTokensPromise]);
tokensSnapshot = results[0];
// Check if there are any device tokens.
if (!tokensSnapshot.hasChildren()) {
return functions.logger.log(
'There are no notification tokens to send to.'
);
}
functions.logger.log(
'There are',
tokensSnapshot.numChildren(),
'tokens to send notifications to.'
);
// Notification details.
const payload = {
notification: {
title: "New Emergency Request!",
body: "Someone needs help check Emergenie App now!",
}
};
// Listing all tokens as an array.
tokens = Object.keys(tokensSnapshot.val());
// Send notifications to all tokens.
const response = await admin.messaging().sendToDevice(tokens, payload);
// For each message check if there was an error.
const tokensToRemove = [];
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
functions.logger.error(
'Failure sending notification to',
tokens[index],
error
);
// Cleanup the tokens who are not registered anymore.
if (error.code === 'messaging/invalid-registration-token' ||
error.code === 'messaging/registration-token-not-registered') {
tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
}
}
});
return Promise.all(tokensToRemove);
});
This seems wring:
const getDeviceTokensPromise = admin.database()
.ref("/Registered Admins/{uid}/Token").once("value");
The {uid} in this string is not defined anywhere, and is also going to be treated as just a string, rather than the ID of a user - which is what I expect you want.
More likely, you'll need to:
Load all of /Registered Admins
Loop over the results you get from that
Get the Token value for each of them
If you are new to JavaScript, Cloud Functions for Firebase is not the easiest way to learn it. I recommend first using the Admin SDK in a local Node.js process or with the emulator suite, which can be debugged with a local debugger. After those you'll be much better equipped to port that code to your Cloud Functions.
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);
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...
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())
})
Using Slackbots(https://www.npmjs.com/package/slackbots) I was hoping to build a simple message forwarding bot (i.e. when the bot receives a direct message, it shoots it into a chat room and says who it is from) but I can't seem to get it to push out user names, only user ID's. Any idea here?
var SlackBot = require('slackbots');
var bot = new SlackBot({
token: 'xoxb-',
name: 'helper'
});
bot.on('start', function() {
var params = {
icon_emoji: ':cat:'
};
bot.postMessageToChannel('helpertest', 'meow!', params);
});
bot.on('message', function(data) {
bot.postMessageToChannel('helpertest', data.text + data.user.name);
})