Discord.js - Set doesn't affect the bot - discord.js

I'm currently in the middle of writing a Quiz Bot and added a little set to make sure the quiz taker doesn't restart mid-quiz.
So I have basically added the set, and upon messaging the bot 'start', it will add the user to the set and won't remove him until 10 minutes later, which is the time span that you have to complete the quiz by.
While running the bot itself, the set doesn't really seem to affect the bot at all, and if you do happen to message him "start" mid-quiz he will restart it without any issues - I have also tested this by attempting at typing 'hello' in the console if the user attempts to do so, but nothing happens yet again.
Any kind of help would be appreciated!
client.on('message', async message => {
const midQuiz = new Set()
const filter = m => m.author.id === message.author.id
let mistakes = 0;
if (message.content === 'start') {
if (message.channel.type == "dm") {
if (midQuiz.has(message.author.id)) {
console.log('hello')
return message.author.send('You're currently mid-quiz! You may not restart it now.')
} else {
midQuiz.add(message.author.id)
setTimeout(() => {
midQuiz.delete(message.author.id)
message.author.send('10 minutes have gone by and you have not completed the quiz, therefore you have failed.')
}, 600000)
const Embed = new Discord.MessageEmbed()
.setTitle("Verification Quiz")
.setDescription("Question #1: What is the owner's name?")
.setColor(3426654)
.setFooter("You may type 'cancel' at any time to stop the quiz.")
message.author.send(Embed)

You're declaring midQuiz inside the message event, making it so that the set gets redeclared and reset every single message.
// outside of the event
const midQuiz = new Set();
client.on('message', ...);

Related

Discord.js move messages in a different Channel

I'm trying to make the t!suggestion command work on different Servers. My problem is that i used the Channel ID in the following Code, which is obviously not working on a second Server. So my thought was to exchange the Channel ID and use the Channel Name. Unfortunatley i have no idea how to do that. And therefore are my question. Is it possible to just change the Code and use the Channel Name instead of the Channel ID and would that work?
bot.on("message", async (message) => {
if (message.content.startsWith("t!suggestion")) {
var str = message.content.slice(" t!suggestion".length);
const channelToSendId = "8786607442820137466";
const embed = new Discord.MessageEmbed()
.setThumbnail("Picture")
.setAuthor("TestBot", "small picture")
.setTitle("Suggestion")
.setColor("#151515")
.addFields({ name: "Person, that suggested somenthing:", value: `<#!${message.author.id}>` }, { name: "suggestion:", value: `${str}` }, { name: "Channel:", value: `${message.channel}` })
.setTimestamp();
await bot.channels.cache
.get(channelToSendId)
.send(embed)
.then((embedMessage) => {
embedMessage.react("✅");
embedMessage.react("❌");
});
message.delete();
}
});
Using the find method will work.
Change this
bot.channels.cache.get(channelToSendId)
To this
bot.channels.cache.find(c => c.name === 'suggestions')
The get method always finds by id (that’s how discord.js sets it), but you can put a condition in find. Note that it is a function. Another problem is that this will still only work on one channel, even if the command is run in a different guild. To fix that, you should only check the current guild's channels.
message.guild.channels.cache.find(c => c.name === 'suggestions')
This will find the channel with the name suggestions (we don’t put the # in the code) and send the message there. Keep in mind you may receive an error if the channel is not found.

Redirect to a channel as well as ping a role then post a message using args

I'm making an announcement command for my bot and want it to go to the announcements channel automatically, then ping the first role mentioned and finally display the message.
For now I have it going to staff commands as a placeholder to get it working, this does not work or throw an error. It properly pings the role, but nothing after that shows up, even the "Debug". What am I missing. I've seen the channel part work but it doesn't for me, and I can't find a similar situation online for the message itself.
module.exports.run = async (client, message, args) => {
if (message.author.bot) return;
let prefix = config.prefix;
if(!message.content.startsWith(prefix)) return;
const channel = message.guild.channels.cache.find(c => c.name === '🚔|staff-cmds');
let pingRole = message.mentions.roles.first();
let messageContent = args.slice(1).join(' ');
channel.send(pingRole, messageContent, "Debug");
This is what happens when the command is run
Try this channel.send(`${pingRole} ${messageContent}`)
The channel.send() function takes only two parameter "content" and "options" you can read more about it here

Discord.js - Getting multiple reactions to execute a command through awaitReactions method

I'm currently in the process of making a little game of TicTacToe, and the idea I had in mind is to make it so instead of the channel being spammed with constant embeds asking for the person's next move, is to simply make it reaction-based, where you get to pick 1 out of 9 reactions (And of course, you wont be able to pick it again if the other player has already picked it).
I have never really worked with requiring multiple reactions, therefore I'd like to ask your help on how exactly to make it so that the message command execution isn't a one-time thing, but will go on until there's eventually a winner.
So far, with the code I have written, this does work 2 times, but then it randomly stops and no longer works.
In addition, when I'm trying to declare a spot as an x or a circle, the spot turns completely blank.
Please help!
The code I have so far:
https://sourceb.in/S7cayfoYjp
Edit: I have now also found that the bot at first kind of skips the whole awaitReactions code. I used 'console.log(i)' for this, so that every time it loops it prints out 'i', and it seemed to be printing out the numbers 0-8 immediately, meaning it's not properly going through the code.
I think what you can use best there is a reactionCollector. It's a temporary reaction listener, attached to a message. A sample code for that would be:
const msg = await message.channel.send('tic tac toe test');
const acceptedEmojis = ['↖️', '⬆️', '↗️', '⬅️', '⏺️', '➡️', '↙️', '⬇️', '↘️']
const filter = (reaction, user) => {
return acceptedEmojis.includes(reaction.emoji.name) && user.id === turnId;
}
//here you create the collector. It has following attributes: it stops after 10 minutes or after 2 minutes of not collecting anything.
const collector = msg.createReactionCollector(filter, { time: 600000, idle: 120000});
//here you start the listener
collector.on('collect', (reaction, user) => {
if (reaction.emoji.name === '↖️') {
//remove the reaction
await msg.reactions.resolve('↖️')
acceptedEmojis.splice(acceptedEmojis.indexOf('↖️'), 1);
//rest of your code...
} else if (reaction.emoji.name === '⬆️') {
...
}
});

Is it possible to have your discord bot kick someone automatically without a mention?

I'm trying to create a bot that kicks someone after they say a specific word, but I don't want to have to mention the user after I say the word. This is my code so far:
case 'ok':
const user = message.member
if (user) {
const member = message.guild.member(user);
if (member){
member.kick('Banned').then(() =>{
message.reply(`Banned`)
})
}
}
break;
}})const PREFIX = '';
Is there some way to make the bot automatically kick the user after they say the case word "ok"?
You could simply use String.prototype.includes(), then kick them using GuildMember.kick() as you are now.
// in your existing message event
client.on('message', (message) => {
if (message.content.includes('ok')) // if the content includes 'ok'
message.member.kick().catch(console.error); // kick the member
});
I would recommend reading the full discord.js guide, as it can help you find a greater understanding of the discord.js library, and how to use it with javascript.

Creating a Discord bot to notify people when to certain text on a website has changed

const Discord = require('discord.js');
const bot = new Discord.Client();
const token = '';
bot.on('ready', () =>{
console.log('This bot is online');
})
bot.on('message', msg=>{
if(msg.content === "?rates"){
msg.reply('.')
}
})
bot.login(token);
This is what I have so far it is very basic, I understand, I'm just trying to get some sort of idea how to process. What I want is as soon as a website gets updated or changed. I would like it to tag everyone and in a certain channel and specifies what has changed. I know this will be a long process but I'm in for the ride :) would appreciate any help.
You need a webhook on the website and listen to it with your bot, if you have control over the site, this may help you, otherwise you could look if the site has one or perhaps ask an owner.
A probably working but not very nice (and not very clean) solution would be to save the text of the website every 5 seconds or so and compare it to the previous save. If it changed, you notify the members through sending a message.

Resources