Hi I am trying to make a color system for my bot but the command creates duplicate roles. Heres the Basics behind my code. I want it to check if the role is already present then don't make it again. How would i do this
const user = message.author;
const member = message.guild.member(user);
if (member.hasPermission('ADMINISTRATOR')) {
message.guild.roles.create({
data: {
name: '#Red',
color: 'ff0000',
permissions: 0,
}
})
message.channel.send('**Red Color Has Been Created!**')
if (message.guild.roles.cache.find(role => role.name == "#Red")) return false; // The role already exists.
message.guild.roles.create({ // Creating the role since it doesn't exist.
data: {
name: "#Red",
color: "#ff0000",
permissions: 0
}
}).then(role => {
message.channel.send(`Role \`${role.name}\` created!`);
});
Related
I am 90% done with my application system but I am missing one thing
I am trying to add roles when someone applies,
I tried doing it with this
let teamRole = message.guild.roles.cache.find(role => role.id == "761996603434598460")
member.roles.add(teamRole)
but it does not add the roles (I don't get any errors doing it)
is there any way I can do it with the code below for the interaction?
client.ws.on("INTERACTION_CREATE", async (interaction) => {
// If component type is a button
if (interaction.data.component_type === 2) {
const guildId = interaction.guild_id;
const userId = interaction.member.user.id;
const buttonId = interaction.data.custom_id;
const member = client.guilds.resolve(guildId).member(userId);
if (buttonId == "send_application") {
// Reply to an interaction, so we don't get "This interaction failed" error
client.api.interactions(interaction.id, interaction.token).callback.post({
data: {
type: 4,
data: {
content: "I have started the application process in your DM's.",
flags: 64 // make the message ephemeral
}
}
});
I would appreciate the help with this
When you add a role it should be a snowflake value, so you should add using the ID and not the role it self
Incorrect:
let teamRole = message.guild.roles.cache.find(role => role.id == "761996603434598460")
member.roles.add(teamRole)
Correct:
// using the ID Directly
member.roles.add('761996603434598460')
Yes I know TLDR but I would appreciate the help
Ok I have this wall of code below I use for applications
It uses a button to start the application (asking questions)
and after the application is filled in it sends that application to a specified applicationChannelId
is there any way I could add buttons to the application that was sent to the applicationChannelId to accept or deny members?
Accept would add a role by id and sends a message to the original applicant
//Not the code I tried using just an example for what the Accept button would do
//
let teamRole = message.guild.roles.cache.find(role => role.id == "761996603434598460")
member.roles.add(teamRole)
member.send("You have been accepted into the team")
Deny would send a message to the original applicant
I have tried doing this for the past few days but just can't get it to work it either does nothing or breaks everything
I have removed some info from the code below to make it shorter
Code for my button I already use to start the application for an applicant
client.on("message", async (message) => {
const reqEmbed = {
color: 0xed5181,
title: 'Blind Spot Team Requirements',
url: 'link',
author: {
name: '',
icon_url: '',
url: '',
},
description: '',
thumbnail: {
url: '',
},
fields: [{
"name": `Read through these Requirements`,
"value": `- requirments will go here`,
}, ],
image: {
url: '',
},
footer: {
text: 'Blind Spot est 2019',
icon_url: 'link',
},
};
//
// Don't reply to bots
let admins = ['741483726688747541', '741483726688747541'];
if (message.content.startsWith(`#blindspot`)) {
message.delete();
const amount = message.content.split(" ")[1];
if (!admins.includes(message.author.id)) {
message.reply("You do not have permission to do that!");
return;
}
// Perform raw API request and send a message with a button,
// since it isn't supported natively in discord.js v12
client.api.channels(message.channel.id).messages.post({
data: {
embeds: [reqEmbed],
components: [{
type: 1,
components: [{
type: 2,
style: 4,
label: "Apply",
// Our button id, we can use that later to identify,
// that the user has clicked this specific button
custom_id: "send_application"
}]
}]
}
});
}
});
Rest of code that handles questions and sends application to applicationChannelId when complete
// Channel id where the application will be sent
const applicationChannelId = "652099170835890177";
// Our questions the bot will ask the user
const questions = ["These are the questions but have deleted them to make this shorter",];
// Function that will ask a GuildMember a question and returns a reply
async function askQuestion(member, question) {
const message = await member.send(question);
const reply = await message.channel.awaitMessages((m) => {
return m.author.id === member.id;
}, {
time: 5 * 60000,
max: 1
});
return reply.first();
}
client.ws.on("INTERACTION_CREATE", async (interaction) => {
// If component type is a button
if (interaction.data.component_type === 2) {
const guildId = interaction.guild_id;
const userId = interaction.member.user.id;
const buttonId = interaction.data.custom_id;
const member = client.guilds.resolve(guildId).member(userId);
if (buttonId == "send_application") {
// Reply to an interaction, so we don't get "This interaction failed" error
client.api.interactions(interaction.id, interaction.token).callback.post({
data: {
type: 4,
data: {
content: "I have started the application process in your DM's.",
flags: 64 // make the message ephemeral
}
}
});
try {
// Create our application, we will fill it later
const application = new MessageEmbed()
.setTitle("New Application")
.setDescription(`This application was submitted by ${member}/${member.user.tag}`)
.setFooter("If the #Username doesn't appear please go to general chat and scroll through the member list")
.setColor("#ED4245");
const cancel = () => member.send("Your application has been canceled.\n**If you would like to start your application again Click on the Apply button in** <#657393981851697153>");
// Ask the user if he wants to continue
const reply = await askQuestion(member, "Please fill in this form so we can proceed with your tryout.\n" + "**Type `yes` to continue or type `cancel` to cancel.**");
// If not cancel the process
if (reply.content.toLowerCase() != "yes") {
cancel();
return;
}
// Ask the user questions one by one and add them to application
for (const question of questions) {
const reply = await askQuestion(member, question);
// The user can cancel the process anytime he wants
if (reply.content.toLowerCase() == "cancel") {
cancel();
return;
}
application.addField(question, reply);
}
await askQuestion(member, "Would you like to submit your Application?\n" + "**Type `yes` to get your Application submitted and reviewed by Staff Members.**");
// If not cancel the process
if (reply.content.toLowerCase() != "yes") {
cancel();
return;
}
// Send the filled application to the application channel
client.channels.cache.get(applicationChannelId).send(application);
} catch {
// If the user took too long to respond an error will be thrown,
// we can handle that case here.
member.send("You took too long to respond or Something went wrong, Please contact a Staff member\n" + "The process was canceled.");
}
}
}
});
at this point I just feel like not even doing this and keep it as is because its driving me insane
You very well can! just send it as you send your normal buttons!
const {
MessageButton,
MessageActionRow
} = require("discord.js"),
const denybtn = new MessageButton()
.setStyle('DANGER')
.setEmoji('ā')
.setCustomId('deny')
const acceptbtn = new MessageButton()
.setStyle('SUCCESS')
.setEmoji('ā')
.setCustomId('accept')
client.channels.cache.get(applicationChannelId).send({
embeds: [application],
components: [new MessageActionRow().addComponents["acceptbtn", "denybtn"]]
});
const collector = msg.createMessageComponentCollector({
time: 3600000,
errors: ["time"],
});
await collector.on("collect", async (r) => {
if (r.user.id !== message.author.id)
return r.reply({
content: "You may not accept/ deny this application",
ephemeral: true,
});
if (r.customId === "acceptbtn") {
let teamRole = message.guild.roles.cache.find(role => role.id == "761996603434598460")
member.roles.add(teamRole)
member.send("You have been accepted into the team")
}
if (r.customId === "denybtn") {
member.send("You have been rejected");
}
});
NOTE: Please be mindful that since your question lacks the functions / method definitions you are using I have used discord.js v13's methods, you may update your discord.js version and the code will work as intended ALTHOUGH the component collector's functions have been directly copy pasted from your question and in some instances such as member#send member is not defined, so please take this as an example and I urge you to write the code instead of copy pasting directly!
I have a bot which is supposed to create a channel under a specific category and then add two users to this channel.
The following bit of code is "supposed" to work and add user two, but fails with DiscordAPIError: Missing Permissions.
What I can't figure out is the actual permission required for this?
function addUserToChannel(ch, user, altCh) {
console.log(user.username,"sendmsg",ch.permissionsFor(client.user).has("MANAGE_CHANNELS", false)); //returns true
if (!ch.permissionsFor(user).has("SEND_MESSAGES", false)) {
console.log("User", user.username, "lacks view-channel permission");
ch.updateOverwrite(user, { //fails here obviously
SEND_MESSAGES: true,
VIEW_CHANNEL: true
});
} else {
console.log("WARN:", user.username, " already in channel ", ch.name);
altCh.send(user.toString() + " join conversation here: " + ch.toString());
}
}
The channel is created using the following code:
function createPrivateChannel(guild, convo, user) {
let everyoneRole = guild.roles.cache.find(r => r.name === "#everyone");
let parent = guild.channels.cache.find(ch => {
//console.log(ch.id,secret.convoCat.id);
return ch.id == secret.convoCat.id;
});
return guild.channels.create(convo.chName, {
type: "text",
parent: parent,
permissionOverwrites: [
{
id: everyoneRole.id, //private channel
deny: ["VIEW_CHANNEL", "SEND_MESSAGES"]
},
{
id: client.user.id, //bot permissions
allow: [ "VIEW_CHANNEL", "SEND_MESSAGES", "READ_MESSAGE_HISTORY", "MANAGE_CHANNELS" ]
},
{
id: user.user.id, //a different user
allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "READ_MESSAGE_HISTORY"]
}
]
});
}
According to the Discord documentation for the 'Edit Channel Permissions' route (which is what updateOverwrite ultimately uses):
Requires the MANAGE_ROLES permission. Only permissions your bot has in the guild or channel can be allowed/denied (unless your bot has a MANAGE_ROLES overwrite in the channel).
I am creating a role reaction bot and I want that if a member has already clicked on a reaction and clicks another it removes the previous one and the role associated with it and gives him the role that corresponds to the last one clicked (there are 15 reactions available) , I was writing code similar to this:
let msg = reaction.message;
let msgGuild = msg.guild;
let userGuild = msgGuild.members.cache.get(user.id);
let userRole = userGuild.roles;
if(reaction.message.channel.id === "764148072498200589") {
if(reaction.emoji.name === "RedRoleID") {
if(userRole.cache.has("BaseRoleID")) {
userRole.remove("BaseRoleID");
userRole.add("RedRoleID");
} else if(userRole.cache.has("OrangeRoleID")) {
userRole.remove("OrangeRoleID");
userRole.add("RedRoleID");
}
};
};
is there an easier and shorter way to do what I want without creating an else if for each role?
I think you could probably make an array of all the emoji names, then use Array.prototype.forEach to check all of them.
let { emoji, message, message: { guild, channel }} = reaction;
let { roles } = guild.member(user.id);
const emojis = ['BaseRoleID', 'RedRoleID', 'OrangeRoleID', 'etc'];
if (channel.id === '764148072498200589') {
emojis.forEach((id) => (roles.cache.has(id) ? roles.remove(id) : id));
roles.add(emoji.name);
}
Edit: My mistake, I thought you had named the emojis as the corresponding role IDs. Here's an alternate method if you do not want to do that:
let { emoji, message, message: { guild, channel }} = reaction;
let { roles } = guild.member(user.id);
const emojis = [
{ emote: 'EmojiID', role: 'RoleID' },
'continue this pattern for all roles and emotes'
];
if (channel.id === '764148072498200589') {
emojis.forEach(({ emote, role }) => {
if (roles.cache.has(role))
return roles.remove(role)
if (emoji.id === emote) roles.add(role)
});
};
This bot I am making is tasked with logging when a user receives a role and when a user has a role removed. It prints that to a desired channel. Additionally I want the bot to send data to a database for the user when they receive a certain role. This part is working. I want the code to IGNORE the database addition/removal when the bot is the one adding or removing roles. This part is not working correctly. The way I have it setup is to check the audit logs to see who the executor of the action was. The intended effect is that if the executor's username matches my bot's username, the code does not run. However, even when the bot adds or removes a user's role, and the audit log visibly shows the bot as the one completing the action, when logging the entry to the console it displays myself, not the bot, as the executor.
client.on("guildMemberUpdate", async function(oldMember, newMember){ //When a user has their roles changed (added or removed)
const entry = await oldMember.guild.fetchAuditLogs({type: 'MEMBER_UPDATE'}).then(audit => audit.entries.first()) //Find audit logs pertaining to member update
console.log(entry) //Print the data to the console
if (entry.executor.username == "PolarBot") { //If the executor of the role addition is the bot then do nothing
return;
}
else { //Otherwise execute the code
if (oldMember.author == client.user) { // Prevent bot from responding to its own messages
return
}
if (oldMember.roles.size < newMember.roles.size) { //If the old member has less roles than the new member
currency.add(oldMember.user.id, 1); //Establish user in the database
const target = oldMember.user.id //Establish target ID for the database
for (const role of newMember.roles.map(x => x.id)) { //Find the new role
const item = await CurrencyShop.findOne({ where: { name: { [Op.like]: `${oldMember.guild.roles.get(role).name}` } } }); //Set item to be the role added
const user = await Users.findOne({ where: { user_id: target } }); //Find the target location in the database and name it user
const items = await user.getItems(); //Ignore this
user.addItem(item); //Add role to the database for that uesr
if (!oldMember.roles.has(role)) { //Check to see if the old member does not have the role
oldMember.guild.channels.find(channel => channel.name === "change-logging").send({embed: { //Send log message to the correct channel
color: 16723502,
title: "**User Roles Updated:**",
description: oldMember.user.tag,
fields: [
{
name: "**New Role(s)**",
value: `${oldMember.guild.roles.get(role).name}`,
inline: true
}
],
timestamp: new Date(),
}})
}
}
}
if (oldMember.roles.size > newMember.roles.size) {
currency.add(oldMember.user.id, 1);
const target = oldMember.user.id
for (const role of oldMember.roles.map(x => x.id)) {
const item = await CurrencyShop.findOne({ where: { name: { [Op.like]: `${oldMember.guild.roles.get(role).name}` } } });
const user = await Users.findOne({ where: { user_id: target } });
const items = await user.getItems();
user.removeItem(item); //Subtract role from the data base
if (!newMember.roles.has(role)) {
oldMember.guild.channels.find(channel => channel.name === "change-logging").send({embed: {
color: 16723502,
title: "**User Roles Updated:**",
description: oldMember.user.tag,
fields: [
{
name: "**Removed Role(s)**",
value: `${oldMember.guild.roles.get(role).name}`,
inline: true
}
],
timestamp: new Date(),
}})
}
}
}
}
});
Expected Result: When logging "entry" to the console the executor section should read my bot's username.
Actual Result: I am listed as the executor.
Link to console output:
Turns out Iām a bit silly and the actual audit log event I wanted to call was MEMBER_ROLE_UPDATE not MEMBER_UPDATE