Kickme Discord.js command - javascript

Im trying to make a command that allows any user to !kickme which will obviously kick them. I would also like the command to send them a dm, im having issues getting started on the kickme part of it, i also cant quite figure out how to get it to dm the specific person that initiated the command, could someone point me in the right way? Below is my current code, although im not sure what good it will do
const Discord = require('discord.js')
module.exports.run = async (bot, message, args) => {
const member = message.guild.member(user);
message.member
}
}
module.exports.help = {
name: "kickme"
}

To message a user you would do this:
message.author.send("you have been kicked")
I would send this before kicking the user to simplify the coding process :)
also please ask for help on errors, trying using the docs or existing threads first

Related

Discord.js Message Collector not working properly, can someone recommend an other way to collect messages or how to make this work?

I made a discord bot using discord.js.
This bot should ask questions about a game when someone uses !rank [rank name]
and if the user answers correctly he/she receives the rank.
I found a way to collect messages, but this method does not want to work, it send the message multiple times and also ignores the time given to the collector, I tried multiple things but neither of them worked, I am very new to creating discord bots so can someone recommend me any other solution to make this bot work?
CODE:
client.on('messageCreate', message => {
if (message.content == '!rank r6') {
message.channel.send('Is Frost an attacker?')
const collector = new Discord.MessageCollector(message.channel, m => m.author.id == message.author.id, {max: 1, time: 8000})
collector.on('collect', message => {
if (message.content == 'no'){
message.channel.send('Role added')
message.member.roles.add('775660517053562900')
}
})
}
})
I'm not sure how you formatted your code to look like that, as the official docs & guide suggest different ways of implementing Collectors. I personally suggest following the guides carefully.
Specifically I see that you try making a new Discord.MessageCollector whereas the guide suggests using a function on message.channel to instate a message collector.
Here are some resources to help you out (assuming you're using Discord.js v12):
https://v12.discordjs.guide/popular-topics/collectors.html#message-collectors
https://v12.discordjs.guide/additional-info/async-await.html#how-do-promises-work

discord.js - guildMemberRemove doesn't work, guildMemberAdd works perfectly fine

Sorry if this is poorly formatted, I've never written a question on here before.
First of all, this is my first time writing anything in JavaScript, so this might be some dumb mistake I'm making that's causing my problem.
What I want to do is send a message when a member joins the server, and send a different message when a member leaves. When someone joins the server, the join message is sent and everything works perfectly fine, but when a member leaves, absolutely nothing happens. I put in some console.logs in the joinMessage and leaveMessage functions, and I get an output for joinMessage but nothing for remove. I have enabled Presence Intent and Server Members Intent on the Discord developer portal.
The join and leave message functions are down at the bottom, but I went ahead and added the entire thing so people could help me better. The bot token and channel IDs have been removed, and they are correct in my version of the code.
console.log('Starting');
const Discord = require('discord.js');
const { Client, Intents } = require('discord.js');
const commandHandler = require("./commands");
const client = new Discord.Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS] });
client.login('token');
var server = client.guilds.cache.get('server');
client.on('ready', ready);
function ready()
{
console.log('Authenticated');
server = client.guilds.cache.get('server');
}
client.on('messageCreate', receivedMessage);
function receivedMessage(msg)
{
commandHandler(msg);
}
client.on('guildMemberAdd', joinMessage);
function joinMessage(member)
{
let channelWelcome = server.channels.cache.get('welcome');
let channelIntro = server.channels.cache.get('intro');
channelWelcome.send(`Welcome to the server, ${member}! Head over to ${channelIntro} and introduce yourself!`);
}
client.on('guildMemberRemove', leaveMessage);
function leaveMessage(member)
{
let channelWelcome = server.channels.cache.get('welcome');
channelWelcome.send(`${member} has left the server.`);
}
I've been trying to figure this out for about an hour, so I'd really appreciate it if someone could help me solve this. Thanks!
You need to include following intent: GUILD_PRESENCES
So your code needs to look like this:
const client = new Discord.Client({ intents: [Intents.FLAGS.GUILD_PRESENCES, Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS] });
Now your bot will detect if a member leaves the guild.
Try calling the function directly in the event parameters. There might be some sort of confliction when extracting the function
client.on("guildMemberAdd", (member) => {
let channelWelcome = server.channels.cache.get('welcome');
let channelIntro = server.channels.cache.get('intro');
channelWelcome.send(`Welcome to the server, ${member}! Head over to ${channelIntro} and introduce yourself!`);
}
client.on("guildMemberRemove", (member) => {
let channelWelcome = server.channels.cache.get('welcome');
channelWelcome.send(`${member} has left the server.`);
}
Also make sure you have set the right intents (if you are running V13), and gave the bot the correct privileged intents in the developer portal
This question/answer is rather old, but I found that the selected answer here is not fully correct, at least in the current version.
You do not in fact need the Presences Intent to receive guildMemberRemove event. The event is actually not firing in cases when the member data is not cached and will therefore not emit the event.
In order to resolve this, you will need to enable Partials on the client, specifically Partials.GuildMember. This will enable the event to emit on un-cached data.

How do I make my command not mention the user?

I just started coding my Discord bot and I made a command that replies to every message that isn’t sent by a bot. When I tried it in DMs it works fine, but when I tried it in my server it would mention the user before the command. In the DM it would say just “test”, but in the server it would say something like “#ExampleUser, test”.
Is there a way I can fix this? Here’s my code:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on("message", (message) => {
if (message.author.bot) return;
return message.reply("test")
});
Instead of message.reply('test') use message.channel.send('test') that sends a message to the channel the original message was sent to.
Solution
use message.channel.send('string'); instead of .reply()
It's very easy :)
Instead of saying message.reply('message') you just have to say message.channel.send('message')

Bot is not defined or Client.channel.get is not a funtion... Why?

I have a little problem. I want to create an announcement command for my Discord bot. I found a "reffer code" in a topic but when I use it, the debug console send me "bot is not defined" or when I replace bot by Client "Client.channels.get" is not a function.. Can you explain me what is wrong?
const msg = message.content;
const announceChannel = bot.channels.get("778027412435763233");
if (cmd === '${prefix}announce') {
let announceEmbed = new Discord.RichEmbed()
.setTitle(":flag_jp: **Annonce** :flag_jp:")
.setDescription(msg)
.setColor("#ff0000");
return announceChannel.send(announceEmbed);
}
First of all, please provide the code in text next time you're trying to get support on here, it's a lot better for us when trying to provide support.
With v12 coming along, the channel objects you're getting must be cached, and it can simply be done using the function:
bot.channels.cache.get('channel id here');
In your example you are using Client.login, so I have to assume that Client is your bot. Then you can simply do:
let announceChannel = Client.channels.cache.get('Channel ID Here');
You are using RichEmbed as well, which means wether you are using an outdated version of discord.js or you don't know that it is MessageEmbed in discord.js v12. Type npm i discord.js#latest in your terminal (or whereever you install packages for your bot). Then replace RichEmbed() with MessageEmbed() and use my first example for the channel.

Discord make channel using bot

I'm making a discord bot, and I'm trying to make use of the createChannel function shown here in the documentation. For some reason, I am getting the following error:
TypeError: bot.createChannel is not a function.
My code is within a function which I pass a message to, and I have been able to create roles and add users to roles within the same function. It's just the createChannel function that's not working. Below is the relevant portions of the code.
const bot = new Discord.Client();
function makeChannel(message){
var server = message.guild;
var name = message.author.username;
server.createRole(data);
var newrole = server.roles.find("name", name);
message.author.addrole(newrole);
/* The above 3 lines all work perfectly */
bot.createChannel(server,name);
}
I have also tried bot.addChannel, and bot.ChannelCreate, since ChannelCreate.js is the name of the file which contains the code for this command. Also, I have attempted specifying channel type and assigning a callback function as well, but the main issue is the TypeError saying that this isn't a function at all. Any idea what I'm doing wrong?
Additionally, I plan to use ServerChannel.update() at some point in the future, so any advice on getting that to work once the previous problem is resolved would be greatly appreciated.
Alright, after a few days of trying things and going through the docs, I have discovered the solution. I am using a more recent version of Discord than the docs I was reading were written for. In the newer version, channels are created with a method in the server, not a client method. so, the code should be:
const bot = new Discord.Client();
function makeChannel(message){
var server = message.guild;
var name = message.author.username;
server.createChannel(name, "text");
}
The "text" value is the type of channel you are making. Can be text or voice.
I'll post a link to the most recent documentation for anyone else who encounters this problem here.
The answer should update documentation link to the GuildChannelManager which is now responsible for creating new channel.
(Example from docs)
// Create a new text channel
guild.channels.create('new-general', { reason: 'Needed a cool new channel' })
.then(console.log)
.catch(console.error);
https://discord.js.org/#/docs/main/stable/class/GuildChannelManager
#Jim Knee's I think your answer is v11, I'm new in discord.js, using Visual Studio Code's auto-code thingy. You can do all the same things except your thing must be this. If you are poor people, getting errors on doing #Jim Knee's answer, this is the place for "YOU!"
Get rid of server.createChannel(name, "text/voice");
And get it to THIS server.channels.create(name, "text/voice");
Hope I can help at least ;)
I'm just a new guy here too
I think you have not logged in with your bot.
From the docs:
const Discord = require('discord.js');
var client = new Discord.Client();
client.login('mybot#example.com', 'password', output); // you seem to be missing this
function output(error, token) {
if (error) {
console.log(`There was an error logging in: ${error}`);
return;
} else
console.log(`Logged in. Token: ${token}`);
}
Alternatively, you can also login with a token instead. See the docs for the example.

Categories