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!
Related
I want to create a delete command where when you right click on a message, it will show under apps "Delete Message" from my bot. For example, Atlas has a set reminder (which seems useless) function under apps [image.] I would like to create this as a guild command. Also, I would like to know how to use the interaction create with this right-click-context-menu function. Thanks!
To register these commands, you need to set the type of the ApplicationCommand to 1 (or ApplicationCommandType.ChatInput). See the docs.
Everything else should stay the same except the type. You can register these commands the usual way, which is documented in the guide here
Here's a snippet:
// token should be your bot's token
const rest = new REST({ version: '10' }).setToken(token);
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
const data = await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
// commands should be an array of ApplicationCommands
{ body: commands },
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
})();
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)
}
})
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!
In Bot Simulator work's perfect
but While Deployemnt ,then we testing this function , Auto Initiate Welcome Message doesn't work .
Checkout Below Screenshot for details .
Give me help please !1 I am stuck this problem
Contact Personally : +91 9786252624
https://i.stack.imgur.com/JZWcY.png
I simply changed the condition on member.id check to === instead of !== and that fixed it for me for both emulator and webchat. One of my bots runs in Teams in addition to these channels, and it does NOT give me the welcome message in that case, which I what I want and expect. Here is the code from my bot.js file.
this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded;
for (let member of membersAdded) {
if (member.id === context.activity.recipient.id) {
await context.sendActivity(welcomeMessage);
}
}
// By calling next() you ensure that the next BotHandler is run.
await next();
});
I working on a login/register system with Node based on the RethinkDB Chat example when I found that it doesn't check if the user exists with email or username something that's a problem.
When I was looking to solve this I was not able to find out why because of running a database check would require a callback with a function something that makes it really hard to achieve.
if (typeof req.user !== 'undefined') {
res.redirect('/account');
return;
}
if (!validateEmail(req.param('email'))) {
req.flash('error', 'Not a valid email address!')
res.redirect('/register');
return;
}
// Add a check for EMAIL/USERNAME here.
if (req.param('password') !== req.param('password2')) {
req.flash('error', 'Passwords does not match!')
res.redirect('/register');
return;
}
What I need help with it to if a user exists with a username or mail that's equal with the one in the form it will send a:
if (alreadyExists) {
req.flash('error', 'That username/email is already in use.')
res.redirect('/register');
return;
}
So the main problem is I have to get to know if it exists in the same functions as the other ones and not in a callback. Any help is appreciated!
The way I usually handle something like this is :
User.filter({username:req.body.username}).run().then(function(userArray){
if(userArray[0]){return res.status(500).json({Error : "Username is in use"});}
I have not run into any issues here using a callback. Is there a specific reason you were trying to avoid it?
Edit : Obviously, replace username in my example with whatever you want to check for, in your case email address. And User here is my user model. I also agree with Tholle about using a POST request. You never want to send user's information/credentials in the query string/URL
To check if a user with the given email address exists, you will have to do a check in your RethinkDB-database, which is asynchronous. This can not be achieved without a callback, but it's not that bad!
var r = require('rethinkdbdash')();
function getUserByEmailAddress(emailAddress) {
return r.db('test').table('user')
.getAll(emailAddress, {index: 'emailAddress'}).run();
}
app.post('/register', function(req, res) {
// User already has a session. Not allowed to log in.
if(req.user) {
return res.redirect('/account');
} else if(!validateEmail(req.body.emailAddress)) {
return res.status(500).send('Not a valid email address');
} else if(req.body.password !== req.body.password2) {
return res.status(500).send('Passwords do not match');
}
getUserByEmailAddress(req.body.emailAddress).then(function(user) {
if(user) {
res.status(500).send('User with given email address already exists');
} else {
// New email address! Encrypt password, add to db, etc.
}
})
}
Note that you will have to create a secondary index for this.
You should probably also consider posting the form with a POST-request instead of a GET-request.