Quick snippet that you can add to your project that allows your bot to give roles to users. To set it up, go ahead and head over the command builder. Once you are there, create a new slash command and title it "give-role". Once you are done, go to the "Options" part and change "String" to "User". You can name it whatever you want. Once you are done, repeat the same process but instead change it to "Role". Then you can enjoy your admin only bot!
// authenticates you with the API standard library
// type `await lib.` to display API autocomplete
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
let user = context.params.event.data.options[0].value;
let role = context.params.event.data.options[1].value;
const now = new Date();
let time = Math.round(new Date(now).getTime() / 1000);
console.log(time);
if (context.params.event.member.permission_names.includes('ADMINISTRATOR')) {
await lib.discord.guilds['@0.2.4'].members.roles.update({
role_id: `${role}`,
user_id: `${user}`,
guild_id: `${context.params.event.guild_id}`,
});
await lib.discord.interactions['@1.0.1'].responses.ephemeral.create({
token: `${context.params.event.token}`,
content: `You gave <@${user}> the role <@&${role}>!`,
tts: false,
response_type: 'CHANNEL_MESSAGE_WITH_SOURCE',
});
await lib.discord.channels['@0.3.2'].messages.create({
channel_id: `${process.env.LOG_CHANNEL}`,
content: ``,
embeds: [
{
type: `rich`,
title: `A role has been added to a user!`,
description: ``,
color: 0x2fcc71,
fields: [
{
name: 'Role',
value: '<@&' + role + '>',
},
{
name: 'User',
value: '<@' + user + '>',
},
{
name: 'Authorizer',
value: '<@' + context.params.event.member.user.id + '>',
},
{
name: 'Time',
value: '',
},
],
},
],
});
} else {
await lib.discord.interactions['@1.0.1'].responses.ephemeral.create({
token: `${context.params.event.token}`,
content: `You must be an Admin to use this command!`,
response_type: 'CHANNEL_MESSAGE_WITH_SOURCE',
});
}