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.
Related
so basically I wanted to make my bot move people to afk as soon as they deafen. and I have a command to make it generate messages in chat, but the question is, can I make it # them as well? and if so how?
code:
const Discord = require("discord.js");
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_VOICE_STATES", "GUILD_MEMBERS", "GUILD_PRESENCES"] });
client.on("voiceStateUpdate", (oldState, newState) =>
{
if (newState.selfDeaf)
{
console.log('User has deafened');
newState.member.voice.setChannel("695840093167943751");
client.channels.cache.get("664487959940169738").send('Undeafen Bitch');
}
To tag a user in discord, the format is <#USERID> so if a user's id is 1, you'd have to include <#1> in what you are sending.
So onto your code, you'd have to change the last line to something like the following:
client.channels.cache.get("664487959940169738").send('<#123> Undeafen Bitch');
But we can't hardcode the id since it'll be unique for each user moved. This should automatically tag the user who got moved:
client.channels.cache.get("664487959940169738").send(`<#${newState.member.id}> Undeafen Bitch`);
command(client, 'explore', (message) => {
const name = message.content.replace('!explore ', '')
message.guild.channels
.create(name, {
type: 'text',
})
.then((channel) => {
const catagoryId = '847950191108554762'
channel.setParent(catagoryId)
})
const channelId = message.guild.channels.id
console.log(channelId)
})
I'm having an issue with saving the newly created channel's id so that way when the channel is done being used instead of typing the name, I can use the id to be inputted into the deletion command. Basically creating temp. predefined or newly found channels and then being able to log the Id and then with a deletion command be able to remove it without writing a name of the channel.
const channelId = message.guild.channels.id
console.log(channelId)
this was my attempt to retrieve/store then log it. I've been trying to do this on my own but have hit a roadblock.
To get a channel’s ID, just type <Channel>.id. There are multiple ways to get a channel, here is an example:
const channelID = <Message>.channel.id;
console.log(channelID);
The above example gets the ID of the message's channel
Also, <guild>.channels.id returns undefined, since guild.channels is a collection of multiple channels, and you can’t just get the id like that. This would be useless if you are trying to get the channel ID since you would have to fetch/get the channel by ID, or use a find method, which could return the wrong channel if there are multiple channels the bot has seen that goes with your condition, so what you are trying to do, it doesn’t make much sense.
Im trying to make discord.js code. When a member joins the voice channnel I want the bot to give them a role, when the member leaves, i want the role to be removed. thanks for your help.
const newChannelID = newState.channelID;
const oldChannelID = oldState.channelID;
if (oldChannelID === "835280102764838942"){
member.send('role given');
let role = message.guild.roles.cache.find(r => r.id === "835279162293747774");
member.roles.add(835279162293747774);
} else if(newChannelID === "835280102764838942"){
member.send('role removed');
member.roles.remove(835279162293747774);
}
})
There's a couple of things you need to clean up in your code. I'm going to assume you're using the voiceStateUpdate event to trigger your command.
Most importantly, (logic-wise) you actually need to flip-flop the way you add and remove roles. Currently, the general code states that if the user left the voice channel (thus oldChannelID = actual vc ID), then it would actually give the user a role. This seems to be the opposite of your intention, thus you'd simply swap out the code in each if/else if statement.
Second, the way you're assigning roles and adding them is incorrect. I'd recommend this resource for more info.
Third, you cannot access message if you were indeed using voiceStateUpdate as your event, since it only emits an oldState and a newState.
Lastly, you need to designate a text channel to send the message to. In my code, I manually grabbed the ID of the channel I wanted and plugged it into the code. You'll have to do the same by replacing the number strings I have with your own specific ones.
With that being said, here's the correct modified code:
client.on('voiceStateUpdate', (oldState, newState) => {
const txtChannel = client.channels.cache.get('803359668054786118'); //manually input your own channel
const newChannelID = newState.channelID;
const oldChannelID = oldState.channelID;
if (oldChannelID === "800743802074824747") { //manually put the voice channel ID
txtChannel.send('role removed');
let role = newState.guild.roles.cache.get("827306356842954762"); //added this
newState.member.roles.remove(role).catch(console.error);
} else if (newChannelID === "800743802074824747") {
txtChannel.send('role given');
let role = oldState.guild.roles.cache.get("827306356842954762"); //change this somewhat
oldState.member.roles.add(role).catch(console.error); //adding a catch method is always good practice
}
})
if(message.content == `${config.prefix}mods`) {
const ListEmbed = new Discord.MessageEmbed()
.setTitle('Mods:')
.setDescription(message.guild.roles.cache.get('813803673703809034').members.map(m=>m.user.tag).join('\n'));
message.channel.send(ListEmbed);
}
Hey so iam making a command which displays all the members with that role but it only seems to be sending 1 of the mods
await message.guild.roles.fetch();
let role = message.guild.roles.cache.find(role => role.id == args[0]);
if (!role) return message.channel.send('Role does not exist'); //if role cannot be found by entered ID
let roleMembers = role.members.map(m => m.user.tag).join('\n');
const ListEmbed = new Discord.MessageEmbed()
.setTitle(`Users with \`${role.name}\``)
.setDescription(roleMembers);
message.channel.send(ListEmbed);
};
Make sure your command is async, otherwise it will not run the await message.guild.roles.fetch(); - this fetches all roles in the server to make sure the command works reliably.
In Discord.jsV12 the get method was redacted and find was implemented.
Aswell as this, I would highly recommend defining variables to use in embeds and for searching since it is much more easy to error trap.
If you have many members with the role, you will encounter the maximum character limit for an embeds description. Simply split the arguments between multiple embeds.
How to check if a user is writing a message in a shared channel and tell him to write in a bot-specific channel?
This is the code I tried:
if (message.channel.id === "bot-playground"){
// write code here
}else {
message.channel.send("Write commands in #bot-playground.").then(msg => {
timeout(3000, msg);
});
}
if (message.channel.id === "bot-playground"){
This needs a channel ID, not a name. To get the ID of the channel, right-click it and select copy ID then use this. if you don't have the copy ID button you may need to Enable developer mode
Also, your mention may not work
message.channel.send("Write commands in #bot-playground.")
See
this question for more
Your issue is caused by the first line if (message.channel.id === "bot-playground") you are trying to use the channel name as an ID.
You either need to look it up using the ID of the channel, which you can get by enabling Developer Mode
Or, you could just use the channel name to find it like so:
message.guilds.channels.cache.find(channel => channel.name === 'bots-playground')
Replace client with whatever variable or constant used to assign the Discord#Client object.
client.on("typingStart", (channel, user) => {
const bot_channel = channel.guild.channels.cache.find(chan => chan.name == "bot-playground");
if (channel.id != bot_channel.id) {
return message.channel.send(`Write commands in <#${bot_channel.id}>`);
}
});