Discord.js -- make presenceUpdate send a message - node.js

im trying to get my super simple bot to send a message stating the user status. as if someone is offline, online,..etc it automatically sends a message to the server saying that happened.
im just doing a work around so it can get updated (i know i need to !status everytime)
anyone has an idea to make it send the same message instantly after presenceUpdate fires?
let userStatus = [];
bot.on("presenceUpdate", (oldMember, newMember) => {
let username = newMember.user.username;
let status = newMember.user.presence.status;
userStatus.push(username, status);
console.log(`${newMember.user.username} is now ${newMember.user.presence.status}`);
})
bot.on('message', (message) => {
// if (!message.content.startsWith(prefix)) return;
if (console.log())
let [username, status] = userStatus;
if (message.content.startsWith(prefix + "status")) {
let botembed = new Discord.RichEmbed()
.setDescription("Status Update")
.setColor("#FFF")
.addField('.............................................', `${username} is now ${status}`);
message.channel.send(botembed);
userStatus = [];
}
});

The issue I think you're running into is that you don't have a direct reference to a channel anymore, which is why you can't "easily" call <TextChannel>.send(...). You'll have to decide which channel you want to send a message to, in your presenceUpdate event listener. Once you decide, you can use this code to get a reference to that channel using the channel's name:
client.on('presenceUpdate', (oldMember, newMember) => {
// get a reference to all channels in the user's guild
let guildChannels = newMember.guild.channels;
// find the channel you want, based off the channel name
// -> replace '<YOUR CHANNEL NAME>' with the name of your channel
guildChannels.find('name', '<YOUR CHANNEL NAME>')
.send('test message!')
.then(msg => {
// do something else if you want
})
.catch(console.error)
});
Note: you don't have to use the channel's name property to identify a unique channel, you can use the channel's id by doing
guildChannels.get('<YOUR CHANNEL ID')
.send('...
Hope this helps!

Related

How edit messages in Discord.js V12 using message ID

There is the way to edit a message on DiscordJS
To do this, we just need to pass it to the function, the client variable.
exports.myNewFunction = async function myNewTestFunction(client)
First, in order to edit a message using the Discord.JS API, we will need to find the server's GuildID.
const guild = client.guilds.cache.get(`Your Guild ID`);
Now, we're going to need to find the channel the message is on. For this, we will use the channel ID
const channel = guild.channels.cache.find(c => c.id === `Your message Channel` && c.type === 'text');
Finally, let's go to the editing part of the message. For this, we will only need to provide the ID of the message that you want to be edited.
channel.messages.fetch(`Your Message ID`).then(message => {
message.edit("New message Text");
}).catch(err => {
console.error(err);
});

how do i collect dms and send them into a channel on a server

I'm trying to make the bot receive DMS and send the received DMS into a channel. It already creates the channel and dms the user on command but idk how to receive & send the received dms in a channel. thanks for helping:)
run(client, message, args) {
message.guild.channels
.create(this.name, { type: 'text', })
.then((channel) => {
const catogaryID = '850020908596592713'
channel.setParent(catogaryID)
})
message.channel.send('<#839172815025340426> s1 needs help')
message.author.send('whats wrong')
What you can do is create a Map which stores the user's name and the channel that you want to post their messages to, something like this:
In your main file somewhere
// Stores the information somewhere that it can easily be retrieved
client.modmailThreads = new Map(); // I'm assuming this is what you want to achieve
Listening to new messages
client.on("message", message => {
// Your other code here
if(message.channel.type == "dm" && client.modmailThreads.get(message.author.id)) { // If this is a DM and the user has a modmail channel
client.channels.cache.get(client.modmailThreads.get(message.author.id)).send(message.content); // Get their modmail channel and send the message to it
}
});
In your current code
run(client, message, args) {
message.guild.channels
.create(this.name, { type: 'text', })
.then((channel) => {
const catogaryID = '850020908596592713'
channel.setParent(catogaryID)
client.modmailThreads.set(message.author.id, channel.id); // Store value
})
message.channel.send('<#839172815025340426> s1 needs help')
message.author.send('whats wrong')
Obviously this is only an example so you may want to do some more modifications yourself.

Send DM when a user joins a VC using Discord.JS

Stack Overflow (I apologize if this question has already been asked, I don't believe it has, but in advance, just in case):
I'm making a discord bot using Discord.JS and Node.JS. I want to make a bot to detect when a user joins a VC, and then sends said user a DM. I have the detection code figured out:
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.login("token");
bot.once('ready', () =>{
console.log(`Bot ready, logged in as ${bot.user.tag}!`);
})
bot.on('voiceStateUpdate', (oldMember, newMember) => {
let newUserChannel = newMember.channelID;
let oldUserChannel = oldMember.channelID;
if(newUserChannel === "channel id")
{
// User Joins a voice channel
console.log("User joined vc with id "+newUserChannel)
}
else{
// User leaves a voice channel
console.log("Left vc");
}
});
But I'm having trouble sending the user (who i assume is represented by newMember) a DM. Can someone kindly provide some assistance? Thanks!
You can use the .member property of the VoiceState instance you get to get the user who has joined the voice channel. Then simply call the .send() method on the voice channel user to send them a DM. Take a look at the example code below:
bot.on('voiceStateUpdate', (oldState, newState) => {
const newChannelID = newState.channelID;
if (newChannelID === "channel id") {
console.log("User has joined voice channel with id " + newChannelID);
const member = newState.member;
member.send('Hi there! Welcome to the voice channel!');
} else {
console.log("User left or didn't join the channel matching the given ID");
}
});
Give it a try and see how it goes

How would one create a guild and then give myself an invite link

so I am currently in the process of trying to have a bot create a new guild/discord server and then console.log() an invite link for that so that I can join the guild and set it up to then invite friends. However, I am running into some slight problems as I am unsure as to how to go about this. My thought process is the following:
run : function(msg, client, cmds, disc, args){
const guild = client.guild.create("New Guild");
let invite //(and here get a new guild invite code )
console.log(invite);
}
My first problem is that I am not even sure if the guild creation is valid and how to get a guild invite from a guild object.
I have semi-completed what I wished to do. However, this is not the final thing because there is an error messaged:
DiscordAPIError: Unknown Channel
at RequestHandler.execute (C:\Users\jonas\node_modules\discord.js\src\rest\RequestHandler.js:154:13)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async RequestHandler.push (C:\Users\jonas\node_modules\discord.js\src\rest\RequestHandler.js:39:14) {
method: 'post',
path: '/channels/801898492082782310/invites',
code: 10003,
httpStatus: 404
I am unsure what the reason for the error in getting the correct channel but I'll reply to this with the full code when I have solved the problem.
run : function(msg, client, cmds, disc, args){
//console.log("1");
client.guilds.create("New Guild")
.then(value => {
let channel = value.channels.cache.first();
let invite = channel.createInvite({
maxAge: 0, // 0 = infinite expiration
maxUses: 0 // 0 = infinite uses
})
.then(result => {
console.log(result);
})
.catch(console.error);
console.log(invite.url);
})
.catch(console.error);
}
The way I fixed it was that previously the object that it fetched was a category channel object meanwhile I needed a text channel/voice channel to create an invite. The solution I used was the following:
run : function(msg, client, cmds, disc, args){
client.guilds.create("New Guild")
.then(value => {
value.channels.create('new-general', { reason: 'Needed a cool new channel' })
.then(channel => {
let invite = channel.createInvite()
.then(invite => console.log(invite))
.catch(console.error);
console.log(invite.url);
})
.catch(console.error);
})
.catch(console.error);
}
To explain the fix, I had to create a channel in the server to invite someone so before I created an invite I created a new channel and used the newly created channel as the channel for the invite.

How to send message using Bale bot into group?

How to send message using Bale bot into group?
for send message using BaleBot, we need to have id of User/Group and have an accessHash. But I can`t get accessHash for send mesage to Group.
It's not too hard, if you have id and access_hash of the group(or channel). you should just make the Peer and after that put it in send_message function.
look at this code. It push a "Hello" to a specified channel after getting start by client.
Note: Remember group and channel are similar.
const SDK = require("balebot");
const BaleBot = SDK.BaleBot;
const TextMessage = SDK.TextMessage;
const Group = SDK.Group;
let bot = new BaleBot('token');
bot.hears(["/start"], (message, responder) => {
console.log(responder._peer)
bot.send(new TextMessage("hello"),responder.peer).then((res) => {
console.log(res)
}).catch((err) => {
console.log(err)
});
});

Resources