Need help fixing a Discord Bot command - node.js

I'm making a >bean {user.mention} command, which the bot would respond with {user.mention} has been beaned! I want the responce to not ping the user, but just say the users username and hashtag (ex: Example#1234).
Here is my code (node.js v12):
if (message.content.startsWith('!bean ')){
message.channel.send('${user} has been beaned!')
}
})

Read docs
Coming to your question
client.on("message", message =>{
if(message.content.startsWith('!bean')){
let target = message.mentions.users.first()
if(!target) {
message.reply(`Mention a user to bean!`)
} else {
message.channel.send(`${target.username} has been beaned`)
//.username returns their username like 'Wanda' & .tag returns their tag like 'Wanda#1234'
}
}
})
Some things you can change to make your future coding organised
• Use a command handler
2 things you must change
• Update to discord.js v13 since v12 is deprecated
• Update your node to v16 or higher
In-case you need help knowing initial setup changes, read the guide

Related

TypeError: Discord.Permissions is not a constructor

There’s this error i get after i try to login to discord using my bot’s dashboard:
> TypeError: Discord.Permissions is not a constructor
Code:
const _perms = guild.permissions_new;
const _checkBot = client.guilds.cache.get(guild.id);
_guilds[index].bot_added = _checkBot ? true : false;
_guilds[index].permissions = new Discord.Permissions(_perms).toArray();
});
delete _user.accessToken;
_user.permissions = _dbUser ? _dbUser.permissions : [];
_user.notifications = _dbUser ? _dbUser.notifications : [];
_user.connections = {};
_user.guilds = _guilds;
Any help would be appreciated. 🙏
I tried to log in my discord bot’s website dashboard via discord connection, while expecting it to work, it just opened up a discord authorization page that doesn’t let you do anything while being refreshed in loop
When in doubt, check the documentation.
Permissions isn't there. Therefore, one shouldn't expect this to work. There is PermissionsBitField, however, and running console.log(new PermissionsBitField(8n).toArray()); returns ["Administrator"]. This is likely what you are looking for.
I searched this specific piece of code in GitHub and found 3 results of which all seem to be the same copy of code and which all appear to be using discord.js 13.x. What very likely happened here is you installed discord.js 14.x (as such is the current latest stable version) and ran version 13 code on it. Unfortunately, this will not work (as evident). There is an update guide you should browse through.

Discord js how to run mp3 when i'm connected to voice channel

client.on(Events.MessageCreate, async message => {
if (message.author.id === '1111111111111111111111111111111' && message.content.slice(-1) === '!') {
const guild = message.guild;
const channels = guild.channels.cache;
let channelToPlayIn;
for (const [channelId, channel] of channels) {
if (channel.type === 2 && channel.members.size > 0) {
channelToPlayIn = channel;
break;
}
}
if (channelToPlayIn) {
const connection = joinVoiceChannel({
channelId: channelToPlayIn.id,
guildId: channelToPlayIn.guild.id,
adapterCreator: channelToPlayIn.guild.voiceAdapterCreator,
selfDeaf: false,
});
setTimeout(() => connection.destroy(), 5_000);
}
}
});
Here is the way i connecte the bot in voice channal and this part work but i don't how to run mp3 file while the bot is connected
I tried use createAudioRessource and createAudioPlayer but none of them seems to work when i tried them.
Thanks in advance
Before you implement some new code take some simple measures:
1. Check if the bot has speak permissions enabled. (admin works as well)
2 Make sure that the createAudioResource/createAudioPlayer are ran after the bot is connected to the channel. (assuming you commence the play once join)
3 Make sure that the .mp3 file you are using actually exists and can be played.
What you can do if these approaches do not work
Similar to the discord.js v13 used here (assuming you are on 13 and not 14): https://www.youtube.com/watch?v=riyHsgI2IDs&ab_channel=CodeLyon
Use a url to a video or google drive where you will run the mp3. v13 and v14 do not work well with mp3s that you have on your computer as the bot must grab it from your computer making this inefficient etc.
Also, if you still want to use mp3s from your own computer, simply don't. The best solution is to simply use urls and if discord.js does not work well with mp3s from a someone's own computer and you must instead install a seperate npm music player to grab mp3s and then play it on the computer.
I hope this helps you and anyone else who happens to see this and have a wonderful day. If it doesn't comment what you want in more detail and if you are using v13 or v14. Also, it would help if you could explain why it must be a mp3 file from your computer if it must be.

Get specific message from specific channel

Purpose: I'm trying to make my bot check for reactions from a specific message.
I've read this post: Get Message By ID: Discord.js
But it didn't help at all.
I've searched the internet to see how can I use .fetchMessage properly. But unfortunately didn't find any results.
This is my code:
client.channels.get('CHANNEL ID').fetchMessage('MESSAGE ID').then(async msg => { *CODE HERE* });
This is the error i get:
TypeError: client.channels.get is not a function
I do realise that client.channels.get is not a function and I should use this in a function but I don't know how to.
Discord.js version: 12.0.2
Node.js verison: 12.13
That answer was for v11, in v12 it has changed to:
client.channels.cache.get(chid).messages.cache.fetch(mesid)
However, it's important to note that client.channels.cache may contain non-text channels, if you are retrieving an ID that you know to be a TextChannel type, you will be fine but if the ID is being retrieved programmatically you need to check to ensure it is an instanceof TextChannel.
In v12 it has changed and uses managers and added this command to my bot and fixed it.
let channelMessage = client.channels.cache.get(channel_id) // Grab the channel
channelMessage.messages.fetch(message_id).then(messageFeteched => messageFeteched.delete({timeout: 5000})); // Delete the message after 5 seconds

How would I make a command that changes a user's nickname when used? (discord.py-rewrite)

I'm trying to create a command for my bot that allows users to change their nickname. I've tried the answer stated here but I get a 'client is not defined' error and when I redefine in within the file itself, it returns a 'client has no 'change_nickname' member' error. The intended usage is supposed to be something like t!callme Jack and the user that used the t!callme command would have their nickname set to 'Jack'. Does anyone know how I could go about this?
Now if you wanted to change the author of the messages nickname, then all you have to do is add the following:
member = ctx.message.author
await member.edit(nick="whatever your heart desires")
member is set to the author of the message, and then we tell the bot that the author of the message (or 'member') will change their nickname to 'whatever your heart desires'.
Make sure that the bot has permission to Manage Nicknames, or else this command will not work and it will give you a '403 FORBIDDEN' error.
It should look something similar like what I provided below:
#bot.command()
async def callme(ctx):
member = ctx.message.author
await member.edit(nick='Nickname wanted')
#This line is used just to keep chat nice and tidy :)
await ctx.message.delete()

Discord.js sending a message to a specific channel

I'm failing to achieve something very simple. I can't send a message to a specific channel. I've browsed trough the documentation and similar threads on stack overflow.
client.channels.get().send()
does NOT work.
It is not a function.
I also do not see it as a method on the Channel class on the official documentation yet every thread I've found so far has told me to use that.
I managed to have the bot reply to a message by listening for a message and then using message.reply() but I don't want that. I want my bot to say something in a specific channel in client.on('ready')
What am I missing?
You didn't provide any code that you already tested so I will give you the code for your ready event that will work!
client.on('ready', client => {
client.channels.get('CHANNEL ID').send('Hello here!');
})
Be careful that your channel id a string.
Let me know if it worked, thank you!
2020 Jun 13 Edit:
A Discord.js update requires the cache member of channels before the get method.
If your modules are legacy the above would still work. My recent solution works changing the send line as follows.
client.channels.cache.get('CHANNEL ID').send('Hello here!')
If you are using TypeScript, you will need to cast the channel in order to use the TextChannel.send(message) method without compiler errors.
import { TextChannel } from 'discord.js';
( client.channels.cache.get('CHANNEL ID') as TextChannel ).send('Hello here!')
for v12 it would be the following:
client.on('messageCreate', message => {
client.channels.cache.get('CHANNEL ID').send('Hello here!');
})
edit: I changed it to log a message.
This will work for the newest version of node.js msg.guild.channels.cache.find(i => i.name === 'announcements').send(annEmbed)
This should work
client.channels.fetch('CHANNEL_ID')
.then(channel=>channel.send('booyah!'))
Here's how I send a message to a specific channel every time a message is deleted. This is helpful if you'd like to send the message without a channel ID.
client.on("messageDelete", (messageDelete) => {
const channel = messageDelete.guild.channels.find(ch => ch.name === 'channel name here');
channel.send(`The message : "${messageDelete.content}" by ${messageDelete.author} was deleted. Their ID is ${messageDelete.author.id}`);
});
Make sure to define it if you're not using messageDelete.

Resources