(discord.js) add role to author - node.js

I am looking for help, my code handles a "message" event, and I am trying to make it add a role to the author of the comment
My current attempt is
client.on('message', msg => {msg.member.addRole("[role id]").catch(function(){});})
however, it does not seem to be working for the various attempts I have made. Any help on this?
Thank you!

Use this code.
Fist check if user don`t have this role, then give him this role
client.on('message', msg => {
if (!msg.member.roles.some(role => role.id === 'YourROLEID')) {
msg.member.addRole('YourROLEID')
.then(console.log(`Succesfuly added role to member ${msg.author.tag}`))
.catch(console.error)
}
})

Related

twitch js bot subscription response

I am writing a bot for the twitch platform, and I want to make it respond to users who have subscribed to the channel.
I found USERNOTICE for this in the documentation https://dev.twitch.tv/docs/irc/commands#usernotice
But I didn't quite understand how to use it.
Something like this?
client.on("message", () => {
client.say(config.get('channel'), '/usernotice message');
});
Just call the /usernotice command and it should respond to this subscription message?
It's just difficult to test all this and I would like to have a more or less specific solution.
UPDATE
Also in old documentation I found this code
chatClient.onSub((channel, user) => {
chatClient.say(channel, `Thanks to #${user} for subscribing to the channel!`);
});
chatClient.onResub((channel, user, subInfo) => {
chatClient.say(channel, `Thanks to #${user} for subscribing to the channel for a total of ${subInfo.months} months!`);
});
chatClient.onSubGift((channel, user, subInfo) => {
chatClient.say(channel, `Thanks to ${subInfo.gifter} for gifting a subscription to ${user}!`);
});
But it throws an error - TypeError: client.onSub is not a function
Is there any way to use it now?
chatClient.on('sub', (channel, user) => {
chatClient.say(channel, `Thanks to #${user} for subscribing to the channel!`);
});
On the assumption you are using tmi.js you should be looking at the tmi.js documentation rather than twitch's documentation. This documentation can be located here
in the documentation they have an example for a subscription event which can be found here:
https://github.com/tmijs/docs/blob/gh-pages/_posts/v1.4.2/2019-03-03-Events.md#subscription
client.on("subscription", (channel, username, method, message, userstate) => {
// Do your stuff.
});

How to get the message content/embed from the message id discord.js?

I want to write a command in my own bot that writes the content of the embedded text to a text channel.
Unfortunately, so far I've only managed to do it for plain text messages
Can someone help me?
I am asking for help here because I am a beginner "programmer" and I am clueless.
Thank you in advance for your help.
module.exports = {
name: 'test',
description: 'test',
execute (channel, message, Discord) {
message.channel.messages.fetch("902919303043637269")
.then(message => message.channel.send(message.content))
.catch(console.error);
}
}
You are getting the message correctly, and it has almost all the properties (since it's fetched). You may however want to change message in the .then to something else (since message is already declared). You can access content and embeds with these 2 properties:
Message.content
Message.embeds
Here is an example logging the content and embeds:
message.channel.messages.fetch("902919303043637269")
.then(msg => {
console.log(msg.content)
console.log(msg.embeds)
})

How to allow certain users to use commands only

I'm trying to make a command on my Discord.JS V12 bot that can only be used by certain users, as a way to protect abuse. How would I be able to make it so only a few users have access to the command.
Basically a command whitelist.
Thanks :]
You must add a line at the start of the command, like :
client.on("message", message =>{
if(!message.content.startsWith(prefix)) return;
if(!message.author.hasPermission("ADMINSTRATOR")) return message.reply("You do not have the permissions.");
if(message.content.startsWith(`${prefix}help`)){
message.channel.send("No help yet.");
};
};
Here, if(!message.author.hasPermission("ADMINSTRATOR")) return; is the line which is required for the limitations...
In a same way, most of the permissions are quoted like that. Manage messages becomes "MANAGE_MESSAGES". and so on
I hope this helps...
If you want it to work with a user id like:
client.on("message", message =>{
if(message.author.id === "User Id"){
if(message.content.startsWith(`${prefix}help`)){
message.channel.send("No help yet.");
};
};
};
and if you want multiple user ids to work with it
client.on("message", message =>{
if(message.author.id === "User Id" || message.author.id === "Second User id"){
if(message.content.startsWith(`${prefix}help`)){
message.channel.send("No help yet.");
};
};
};
You could also put all of the user ids in a const and refer to them in the command as well
You Can Add Permissions Example : You Can Set Adminstrator Permission To Use That Command Or You Can Set A Role To Use That Command!

How to send welcome message AND load a specific dialog automatically in Microsoft Bot Framework v.3 (Node.js)?

I'm trying both to show a welcome message when my bot starts up and also load a specific dialog. We are using version 3 in the company where I'm working (I know, it's old and not supported).
As far as the welcome message, https://learn.microsoft.com/en-us/azure/bot-service/nodejs/bot-builder-nodejs-handle-conversation-events?view=azure-bot-service-3.0 says to use on conversationUpdate, which works fine, but this seems to be contradicted by https://blog.botframework.com/2018/07/12/how-to-properly-send-a-greeting-message-and-common-issues-from-customers/, which suggests one should not use conversationUpdate, except when using DirectLine, but instead send an event. Is this the final word on the matter? Is there a better way?
I'd also like to load a dialog automatically after the welcome message. How do I do this? Can I access the session during the 'on conversationUpdate' event above and load the dialog directly there? Is there a better way?
Thanks for any help!
It is contradictory, but conversationUpdate is likely your best bet in most situations. However, because channels handle this differently, you should be aware that the result can vary. For direct line, it is a better option to utilize sending events.
An example, in case of need:
bot.on('conversationUpdate', function(message) {
if (message.membersAdded) {
message.membersAdded.forEach(function(identity) {
if (identity.id === message.address.bot.id) {
var reply = new builder.Message()
.address(message.address)
.text("Welcome");
bot.send(reply);
}
});
}
});
For immediately calling a specific dialog, do this:
bot.on('conversationUpdate', function (message) {
if (message.membersAdded) {
message.membersAdded.forEach(function (identity) {
if (identity.id === message.address.bot.id) {
bot.beginDialog(message.address, '/main');
}
});
}
});
bot.dialog('/main', [
function (session, args, next) {
session.send("Glad you could join.");
session.beginDialog('/next');
}
]);
Simply combine the two for sending the welcome message and starting up a dialog.
Hope of help!

How to kick on specific message

I'm trying to auto-kick people with my discord bot when they send an invite link, but message.author.kick() doesn't seem to work. I've also tried other variations of it, like member.kick().
This is my code so far:
client.on('message', message => {
if (message.content.includes('discord.gg/')) {
message.channel.send('Nope');
message.delete(3000);
message.author.kick('posting links');
}
});
.author gives a User object that you can't kick. You have to kick a GuildMember: you can obtain the author's member object by using message.member.
Here is the correction of your code:
client.on('message', message => {
if (message.content.includes('discord.gg/')) {
message.channel.send('Nope');
message.delete(3000);
message.member.kick('posting links');
}
});

Resources