I want that the bot gives the reacter a role. But it doesnt work. Does anyone know where the problem is? Im coding in discord.js v12
if (user.bot) return;
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
if (reaction.message.guild.id !== "753289194738024632") return;
if (reaction.message.channel.id === "753290581412937832") {
const cs = bot.emojis.cache.find(emoji => emoji.name === "csgo");
const vl = bot.emojis.cache.find(emoji => emoji.name === "valorant");
const fn = bot.emojis.cache.find(emoji => emoji.name === "fortnite");
let role = reaction.guild.roles.cache.find(role => role.id === '753290549548679229');
member.roles.add(role.id);
Try changing reaction to message
let role = message.guild.roles.cache.find(role => role.id === '753290549548679229');
I think reaction.guild isn't a valid function
I would change it to this:
if(message.channel.type === 'dm') return message.reply('This command does not work in DMs') //checks if the command wasnt executed in DMs
let role = message.guild.roles.cache.find(role => role.id === '753290549548679229');
Related
I should know how to do this. But the code below does not work.
The goal is to remove roles when the user adds a reaction to a message.
client.on('messageReactionAdd', async (reaction, user) => {
const message = await reaction.message.fetch(true);
const channelStockSettings = '961958948976607243';
const fundamentalPlays = 'βοΈ';
const fundamentalPlaysRoleSE = message.guild.roles.cache.find(role => role.name === 'βπΈπͺ');
const fundamentalPlaysRoleUS = message.guild.roles.cache.find(role => role.name === 'βπΊπΈ');
const longTerm = 'β°';
const longTermRoleSE = message.guild.roles.cache.find(role => role.name === 'β°πΈπͺ');
const longTermRoleUS = message.guild.roles.cache.find(role => role.name === 'β°πΊπΈ');
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
if (reaction.message.channel.id === channelStockSettings) {
if (reaction.emoji.name === fundamentalPlays) {
await reaction.message.guild.members.cache.get(user.id).roles.remove(fundamentalPlaysRoleSE);
await reaction.message.guild.members.cache.get(user.id).roles.remove(fundamentalPlaysRoleUS);
} else if (reaction.emoji.name === longTerm) {
await reaction.message.guild.members.cache.get(user.id).roles.remove(longTermRoleSE);
await reaction.message.guild.members.cache.get(user.id).roles.remove(longTermRoleUS);
} else {
return;
}
}
});
This is the way I've coded this before. What the ... is wrong?
After testing your code, it appears that the issue is that you have the wrong unicode emotes, assuming you're trying to use the default Discord emotes for star and mountain. In other words, reaction.emoji.name === fundamentalPlays and reaction.emoji.name === longTerm both were returning false.
I'm not sure how this happened or where you got these unicode emotes from (perhaps an older version of discord, or from a third-party website?), but neither correctly matches the Discord unicode emote (the star emotes seem to look exactly the same but are not equal, and the mountain emotes both do not look the same and are not equal). The best way to get the unicode form of any Discord emote is to put a backslash before a Discord emote when sending it. Then, copy the unicode emote that it gives you.
Here's your code, modified to have the correct unicode emotes. I tested it, and it is working for me:
client.on('messageReactionAdd', async (reaction, user) => {
console.log("Reaction received");
const message = await reaction.message.fetch(true);
const channelStockSettings = '883731756438671391';
const fundamentalPlays = 'β';
const fundamentalPlaysRoleSE = message.guild.roles.cache.find(role => role.name === 'Member');
const fundamentalPlaysRoleUS = message.guild.roles.cache.find(role => role.name === 'Youtuber');
const longTerm = 'β°οΈ';
const longTermRoleSE = message.guild.roles.cache.find(role => role.name === 'Testor');
const longTermRoleUS = message.guild.roles.cache.find(role => role.name === 'Co-op');
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
if (reaction.message.channel.id === channelStockSettings) {
if (reaction.emoji.name === fundamentalPlays) {
await reaction.message.guild.members.cache.get(user.id).roles.remove(fundamentalPlaysRoleSE);
await reaction.message.guild.members.cache.get(user.id).roles.remove(fundamentalPlaysRoleUS);
} else if (reaction.emoji.name === longTerm) {
await reaction.message.guild.members.cache.get(user.id).roles.remove(longTermRoleSE);
await reaction.message.guild.members.cache.get(user.id).roles.remove(longTermRoleUS);
} else {
return;
}
}
});
Note that there could be numerous additional issues responsible for this code not working, however:
a) The messageReactionAdd event only fires for reactions added to cached messages. Therefore, it will not fire if you add a reaction to a message sent before the bot started. If you are trying to do this with such an uncached message, you will need to specifically fetch the message immediately when your bot starts up (most likely in your ready event handler).
b) Make sure you have the correct intents for receiving messages and message reactions. I assume you do, but double-check, otherwise the messageReactionAdd event will not fire at all.
I had the same problem with my bot. Can you try delete the embed message and add it again. Then try it again.
I'm trying to set up a command that when mentioning a member gives them a specific role. However every time I get the same roles undefined error. Can anyone help me out?
My code:
client.on('messageCreate', (message) => {
if (message.content == '!brig'){
const role = message.guild.roles.cache.find(role => role.name === 'BRIG')
const target = message.mentions.members.first();
target.roles.add('757816527478325391')
}
})
I made a small modification to your code changing message.content === '!brig' to message.content.includes("!brig") and it seems like it works fine for me. You can try my code and let me know if anything changes:
const { Client } = require("discord.js");
const client = new Client({
intents: ["GUILDS", "GUILD_MESSAGES"],
});
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}`);
});
client.on("messageCreate", (message) => {
if (message.content.includes("!brig")) {
const role = message.guild.roles.cache.find(
(role) => role.name === "BRIG"
);
const target = message.mentions.members.first();
target.roles.add(role.id);
}
});
client.login("YOURTOKEN");
Edit: This code is working with discord.js#13.5.0 but I can't see why it wouldn't work with the latest version (13.6.0 at this moment).
So I am trying to setup an event that creates a channel when someone reacts to the message, and deletes it when they unreact to it. Here is the code
module.exports = {
name: 'ticket',
description: "Sets up a reaction role message!",
async execute(message, args, Discord, client) {
const channel = '799546836327202877'
const TicketMade = 'π©'
let embed = new Discord.MessageEmbed()
.setColor('#e42643')
.setTitle('React with the icon below to open a ticket!')
let messageEmbed = await message.channel.send(embed);
messageEmbed.react(TicketMade);
client.on('messageReactionAdd', async (reaction, user) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
message.guild.channels
.create('ticket', {
type: 'text',
})
.then((channel) => {
const categoryId = '800219980561776680'
channel.setParent(categoryId)
})
})
client.on('messageReactionRemove', async (reaction, user) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
message.guild.channels
.delete('ticket')
})
}
The error I get when I try to unreact is: (node:32604) UnhandledPromiseRejectionWarning: TypeError: message.guild.channels.delete is not a function. I've looked all over for how to fix it but I couldn't find any solutions.
The simple reasoning for your issue is that .delete() while talking about channels is Channel Object based. Meaning you'll first have to find/get the channel you're looking for using its name/ID, turn it into an object, and then you could use the .delete() function, this can be done as the following:
client.on('messageReactionRemove', async (reaction, user) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
/*
const channel = await message.guild.channels.cache.find(ch => ch.name === 'ticket');
*/
const channel = await message.guild.channels.cache.get('channel id here')
channel.delete();
})
So here I want my music bot to leave a vc when no one is in the vc anymore. It does work but someone it repeats the leaving process twice. I have tried tinkering it but couldn't find the problem why it would leave twice! Here is my code:
const ytdl = require("ytdl-core-discord");
exports.run = async (client, message, args, ops) => {
const voiceChannel = message.member.voice.channel;
if (!message.member.voice.channel) return message.channel.send("Hm... I don't see you in a Voice Channel! Please connect to a voice channel before executing this command!");
if (message.guild.me.voice.channel) return message.channel.send("Smh! I am already playing in another vc!");
let validate = await ytdl.validateURL("Some Link");
if(!validate) return message.channel.send("This stream seems to be unaivalable, please try again later!");
let connection = await message.member.voice.channel.join();
let dispatcher = await connection.play(await ytdl("Some Link"), { type: 'opus' });
message.channel.send('Now Playing Some **Some Stream** by Some Person');
client.on('voiceStateUpdate', (oldMember, newMember) => {
let newUserChannel = newMember.voiceChannel
let oldUserChannel = oldMember.voiceChannel
if(oldUserChannel === undefined && newUserChannel !== undefined) {
// User Joins a voice channel
console.log("Staying in VC");
} else if(newUserChannel === undefined){
try{
if (voiceChannel) {
setTimeout(function() {
console.log("Leaving a VC!")
message.guild.me.voice.channel.leave();
}, 3000)
}
} catch(e) {
console.log(e);
}
}
})
}
There was a few issues with your code, with newUserChannel and oldUserChannel you should change it to.
let newUserChannel = newMember.channel
let oldUserChannel = oldMember.channel
And futher down when you're checking the status of oldUserChannel and newUserChannel instead of checking it against undefined, check it against null:
if(oldUserChannel === null && newUserChannel !== null) { ... }
And within your try...catch you should change the sanity check to newUserChannel
try {
if (newUserChannel) {
Full Code
bot.on('voiceStateUpdate', (oldMember, newMember) => {
let newUserChannel = newMember.channel
let oldUserChannel = oldMember.channel
if(oldUserChannel === null && newUserChannel !== null) {
console.log("Staying in VC");
} else if(newUserChannel === null){
try{
if (newUserChannel) {
setTimeout(function() {
console.log("Leaving a VC!")
message.guild.me.voice.channel.leave();
}, 3000)
}
} catch(e) {
console.log(e);
}
}
});
Ok so... I've been attempting to make a reaction roles command but it won't work. The role isn't given to the user. It gives me this error "ReferenceError: reaction is not defined"
Code:
const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL","REACTION"]});
client.on('message', async message => {
if(message.content === '>reactions'){
const embed = new Discord.MessageEmbed()
.setTitle('Reaction Roles')
.setDescription('React to obtain your roles!')
.setColor('#242323')
let MessageEmbed = await message.channel.send(embed)
MessageEmbed.react('π¨βπ¦')
}
if(reaction.message.partial) await reaction.message.fetch();
if(reaction.partial) await reaction.fetch();
if(user.client) return;
if(!reaction.message.guild) return;
if(reaction.message.channel.id === "759064796611215442") {
if(reaction.emoji.name === 'π¨βπ¦'){
await reaction.message.guild.member.cache.get(user.id).roles.add('760838222057046046')
}
}
});