I found a way where a bot send me DM when I write !whitelist in my server but when I want to talk with the bot in DM it's not working
client.on("message", async message => {
if (message.content === "!whitelist") {
message.guild.members.cache.forEach((member) => {
if (member.roles.cache.has(staffID)) {
member.send(`Hey ! Congratulation you are whitelisted ! `);
if (message.content === "!speak")
{
member.send(`Hi how are you ?`);
}
}
})
}
})
I think you're expecting a reply from the user in dm's
If that's the case you'll have to use a collector or awaitMessages
Your code is also in djs v12; I recommend you to update to djs v13 for support and more features
client.on("message", async message => {
if (message.content === "!whitelist") {
message.guild.members.cache.forEach((member) => {
if (member.roles.cache.has(staffID)) {
member.send(`Hey ! Congratulation you are whitelisted ! `).then(msg => {
const filter1 = m => m.author.id === message.author.id
msg.channel.awaitMessages({filter: filter1, time: 5 * 12000, max: 1}).then(messages =>{
let msg1 = messages.first().content
if(msg1 === "!speak"){
member.send('Hi how are you?')
}
})
})
}
})
}
})
Related
I have recently spent a long time making a discord ticket bot, and all of a sudden now, it isnt turning on. There are no error messages when I turn the bot on, and the webview says the bot is online. I am hosting on repl.it
Sorry for the long code, but any genius who could work this out would be greatly appreciated. Thanks in advance.
const fs = require('fs');
const client = new Discord.Client();
const prefix = '-'
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles){
const command = require(`./commands/${file}`)
client.commands.set(command.name, command);
}
require('./server.js')
client.on("ready", () => {
console.log('Bot ready!');
client.user.setActivity('-help', { type: "LISTENING"} ).catch(console.error)
})
client.on("message", async (message) => {
if (message.author.bot) return;
const filter = (m) => m.author.id === message.author.id;
if (message.content === "-ticket") {
let channel = message.author.dmChannel;
if (!channel) channel = await message.author.createDM();
let embed = new Discord.MessageEmbed();
embed.setTitle('Open a Ticket')
embed.setDescription('Thank you for reaching out to us. If you have a question, please state it below so I can connect you to a member of our support team. If you a reporting a user, please describe your report in detail below.')
embed.setColor('AQUA')
message.author.send(embed);
channel
.awaitMessages(filter, {max: 1, time: 1000 * 300, errors: ['time'] })
.then ( async (collected) => {
const msg = collected.first();
message.author.send(`
>>> ✅ Thank you for reaching out to us! I have created a case for your inquiry with out support team. Expect a reply soon!
❓ Your question: ${msg}
`);
let claimEmbed = new Discord.MessageEmbed();
claimEmbed.setTitle('New Ticket')
claimEmbed.setDescription(`
New ticket created by ${message.author.tag}: ${msg}
React with ✅ to claim!
`)
claimEmbed.setColor('AQUA')
claimEmbed.setTimestamp()
try {
let claimChannel = client.channels.cache.find(
(channel) => channel.name === 'general',
);
let claimMessage = await claimChannel.send(claimEmbed);
claimMessage.react('✅');
const handleReaction = (reaction, user) => {
if (user.id === '923956860682399806') {
return;
}
const name = `ticket-${user.tag}`
claimMessage.guild.channels
.create(name, {
type: 'text',
}).then(channel => {
console.log(channel)
})
claimMessage.delete();
}
client.on('messageReactionAdd', (reaction, user) => {
const channelID = '858428421683675169'
if (reaction.message.channel.id === channelID) {
handleReaction(reaction, user)
}
})
} catch (err) {
throw err;
}
})
.catch((err) => console.log(err));
}
})
client.login(process.env['TOKEN'])
Your problem could possibly be that you have not put any intents. Intents look like this:
const { Client } = require('discord.js');
const client = new Client({
intents: 46687,
});
You can always calculate your intents with this intents calculator: https://ziad87.net/intents/
Side note:
If you are using discord.js v14 you change client.on from message to messageCreate.
i have looked around for an answer but no dice. bot starts and pulls the required thumbnail but fails to stop when the stop command is issued:
client.on('messageCreate', async (message) => {
if (message.content && message.content.toLowerCase() === 'play baka radio') {
if (!message.member.voice?.channel) return message.channel.send('You need to be a voice channel to execute this command')
const connection = joinVoiceChannel({
channelId: message.member.voice.channelId,
guildId: message.guildId,
adapterCreator: message.guild.voiceAdapterCreator
})
const player = createAudioPlayer()
const resource = createAudioResource('https://nrf1.newradio.it:10328/alfaanime')
connection.subscribe(player)
message.channel.send({ content: "Now Playing", embeds: [embed2], files: ['./logo/bkrlogo.png'] }) + player.play(resource)
if (message.content && message.content.toLowerCase() === 'stop')
player.stop(resource)
}
You will have to read this : https://discordjs.guide/popular-topics/collectors.html#await-messages its very usefull while working with discord bots
Here I ask my bot to watch the origin message (play baka radio) channel for a message who fit to my filter filter, when someone send "stop", the function is triggered and the player is instantly stopped
const createPlayer = async (message) => {
const connection = joinVoiceChannel({
channelId: message.member.voice.channelId,
guildId: message.guildId,
adapterCreator: message.guild.voiceAdapterCreator,
});
const player = createAudioPlayer();
const resource = createAudioResource(
"https://nrf1.newradio.it:10328/alfaanime"
);
connection.subscribe(player);
player.play(resource);
await message.channel.send({
content: "Now Playing",
});
const filter = (response) => response.content === "stop";
await message.channel
.awaitMessages({ filter, max: 1, time: 999999, errors: ["time"] })
.then((askedStop) => {
player.stop(resource);
});
};
Here I just call my function createPlayer
client.on("messageCreate", async (message) => {
if (message.content && message.content.toLowerCase() === "play baka radio") {
if (!message.member.voice?.channel)
return message.channel.send(
"You need to be a voice channel to execute this command"
);
createPlayer(message);
}
});
You can choose to set time in the awaitMessages, or not
In my previous question, I was recommended to try reconnecting to the port, and I have researched and have not been able to find out how. How can I do this?
const express = require("express");
const app = express();
app.listen(3000, () => {
console.log("Project is running!");
})
app.get("/", (req, res) => {
res.send("Hello World!");
})
const Discord = require("discord.js");
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
client.on("messageCreate", message => {
if(message.content === "jobs") {
message.channel.send("fortnite battle royale");
}
})
client.on("messageCreate", message => {
if(message.content === "ping") {
message.channel.send("pong, you idiot, you should know the rest");
}
})
client.on("messageCreate", message => {
if(message.content === "pls dont help") {
message.channel.send(message.author.toString() + " " +"ok, i wont, all you had to do was ask... also dank memer stole this command");
}
})
client.on("messageCreate", message => {
if (message.author.bot) return;
if(message.content.includes("dream")) {
var msgnumber= (Math.floor((Math.random() * 2) + 1));
console.log(msgnumber);
if (msgnumber===1) {
message.channel.send("did someone say dream!?");
} else if (msgnumber===2) {
message.channel.send("why we talkin' about dream... huh!?");
}
}
})
client.on('messageCreate', message => {
if (message.content === '!ping') {
message.channel.send(`🏓Latency is ${Date.now() - message.createdTimestamp}ms. API Latency is ${Math.round(client.ws.ping)}ms`);
}
});
client.on("messageCreate", message => {
if(message.content === "username") {
message.channel.send(message.author.username);
}
})
client.on('ready', () => {
console.log('Bot is online!');
client.user.setActivity('not being entertained', {type : 'PLAYING'} )
})
client.login(process.env.token);
Comment that suggested this: My bot has been offline with no error, it was working yesterday
I'm trying to get my bot join a vc that a user is in, when saying "+sing". After the audio is finished the bot should disconnect from the vc. This is as far as i've gotten, I don't know what I have done wrong.
client.on("message", msg => {
if (message.content === '+sing') {
const connection = msg.member.voice.channel.join()
channel.join().then(connection => {
console.log("Successfully connected.");
connection.play(ytdl('https://www.youtube.com/watch?v=jRxSRyrTANY', { quality: 'highestaudio' }));
}).catch(e => {
console.error(e);
connection.disconnect();
})
});
Btw I'm using discord.js.
You can get their GuildMember#VoiceState#channel and use that as the channel object to join
client.on("message", msg => {
if (message.content === '+sing') {
// you should add a check to make sure they are in a voice channel
// join their channel
const connection = msg.member.voice.channel.join()
}
})
I am looking for a way to make a Discord bot which either reacts or replies to only certain users. It can choose the user by either role or ID, but I can not seem to get it working. This is what I have tried:
if (message.author.id === 'myDiscordID') {
message.reply('hello!').then(r => {
});
}
I am coding in Discord JS, if that helps. This is the entire index.js file:
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
const client = new Discord.Client();
client.once('ready', () => {
console.log('Ready!');
});
client.on('message', message => {
if (message.author.id === 'myDiscordID') {
message.reply('hello!').then(r => {
});
}
});
client.login(token);
The file runs fine, the bot comes online, and then it prints 'Ready!' to the console, however, the rest of the code doesn't seem to work.
I`ts look like this must work.
Are you sure bot in your server and have permissions to read message ?
Try this ready block
client.once('ready', () => {
console.log('Ready!');
const guildList = client.guilds.cache.map(guild => guild.name)
console.join(`Bot go online in ${guildList.length}`)
console.log(guildList.join('\n'))
});
And some solutin to check user.id or roles includes:
console.log(message.content) for check is this event triggeret.
client.on('message', message => {
console.log(message.content)
if (message.author.id === '') {
message.reply('author contain')
}
if (message.member.roles.cache.has('ROLE ID')) {
message.reply('role contain')
}
if(message.member.roles.cache.some(role => ['ID1', 'ID2'].includes(role.id)) {
message.reply('some role contain')
}
});
I was also having a problem with that. Here is the solution that worked for me.
const userID = "The_user's_id";
bot.on("message", function(message) {
if(message.author.id === userID) {
message.react('emoji name or id');
}
});
Note: to find the id of a user, just right click him and press "copy ID".