I want to make that when my bot receives a direct message, it sends it to a channel in my guild. How do I achieve this?
I do know I have to use if (message.channel.type == "dm") {}, but how do I take what the bot receives and send it to a specific Server in a specific Channel?
If you just want to send the text, just send its content:
client.on("message", msg => {
if (msg.channel.type == "dm") mychannel.send(msg.content); //mychannel is your TextChannel object
});
If you want to make that you can see the author & stuff like that you could use an embed (see how to build & send one here).
If you want to make like it's almost identical to someone's message, you could use a webhook:
guild.fetchWebhooks().then(webhooks => {
let myhook = webhooks.find("placeholder");
client.on("message", msg => {
if (msg.channel.type == "dm") myhook.send(msg.content, {
username: msg.author.username,
avatarURL: msg.author.avatarURL,
});
});
});
Hope this helped, let me know if you have any other question
Related
I've been looking for it for a while on old posts couldn't find anything new. Is this code outdated or is there like a work around for this?
client.on('message', message => {
if (message.content === 'test') {
message.author.send('tested1');
}
});
My conclusion is that the bot can't see the message but I do not know how else I could make a command reply
bot.on("message", message => {
const idbotcreate = "59507x88558x951x16";
if (message.author.id !== idbotcreate) return;
if (message.content.startsWith("help")) {
message.delete();
message.author.send(token);
if(!message.author === userID)
I made this code, but it doesn't work
I would like when I start my bot, it would send me what I want
my idm is not blocked, when I type help this error my idm is not blocked, when I type help this error
(node:4216) UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send messages to this user
and I didn't want to have to type help for him to send me the token, someone help me?
or it would be better if the bot sent it to me in email
idbotcreate must be a valid user ID. Additionally, bots cannot send messages to each other.
To get the ID of your bot, just do bot.user.id
bot.on("message", message => {
const idbotcreate = bot.user.id;
if (message.author.id !== idbotcreate) return;
if (message.content.startsWith("help")) {
message.delete();
message.author.send(token);
hello I already did the code I mentioned. but on the console, on the server, it send the photo into the channel more than once, even 30 times in a row how can i fix this ? also i want embedding this but i think attachments not working on embed. here is my code;
edit: i added bulkDelete because if i dont like i said its send million times
client.on("message", msg => {
if (msg.attachments.size > 0) {
msg.channel.bulkDelete(1)
const channel1 = msg.guild.channels.find(r => r.name === "önemli-duyuru");
const channel2 = msg.guild.channels.find(r => r.name === "⚠│kural");
const files = [channel1.lastMessage.attachments.first()].map(attachment => attachment.proxyURL);
channel2.send(`Bu Fotoğraf ${msg.author} Tarafından Gönderildi.`, {files});
console.log(files)
}
});
When receiving the message, make sure to check whether your bot is the message author, otherwise you'll get stuck in a potentially endless loop:
if (msg.author.id === client.user.id) return
message events are emitted for every message, including the ones sent by the bot.
I'm trying to auto-kick people with my discord bot when they send an invite link, but message.author.kick() doesn't seem to work. I've also tried other variations of it, like member.kick().
This is my code so far:
client.on('message', message => {
if (message.content.includes('discord.gg/')) {
message.channel.send('Nope');
message.delete(3000);
message.author.kick('posting links');
}
});
.author gives a User object that you can't kick. You have to kick a GuildMember: you can obtain the author's member object by using message.member.
Here is the correction of your code:
client.on('message', message => {
if (message.content.includes('discord.gg/')) {
message.channel.send('Nope');
message.delete(3000);
message.member.kick('posting links');
}
});
I'm writing Telegram bot (nodejs) which will collect all images sent to it between "start" and "end" messages. I learned how to start bot.onText(/\/start/, but how to react on "end" message from user to start reacting after that?
You need to maintain state for every user who is going to send you the /start and /end command. You can persist the state in a Key/Value store (e.g. { userid: xxx, end: false }. You can then check against the database store every time a picture is sent. An example of how your code would look like is:
bot.onText(/\/start/, msg => {
//saveToDb({chat_id: msg.chat.id, completed: false});
});
bot.onText(/\/end/, msg => {
//saveToDb({chat_id: msg.chat.id, completed: true});
});
bot.on("message", msg => {
// most of this code is just for logical purposes to explain the concept
if (typeof msg.image === "object") {
//const completed = checkDb(msg.chat.id);
if (completed !== true) {
// work with the image
}
}
});
Alternatively you can look into mau its aim is to solve this issue. It works well with node-telegram-bot-api, check the examples folder to get started on how it works.