Mute and Unmute people on all servers. It also dms the muted/unmuted user. Simply run the code and the command will be created. Enjoy! NOTE:global commands take upto an hour to register because Discord has to check it on every server.
// 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 firstCheck = await lib.utils.kv['@0.1.16'].get({//get the value of key MuteCommand-Check which will be used later
key: `MuteCommand-Check`,//Key name
defaultValue: true,//Default value of the key
});
if (firstCheck) {//Check if the command is there
await lib.discord.commands['@0.0.0'].create({
name: "toggle-mute",//Name of the command
description: "Mute / Unmute a User",
options: [
{
type: 9,//option type, type 9 is mentionable
name: "user",//option name
description: "User who you want to mute / unmute",//option description
required: true//true = option required, false = not required
},
{
type: 3,//option type, type 3 is string
name: "reason",//option name
description: "reason why you want to mute / unmute",//option description
required: true//true = option required, false = not required
}
]
});
await lib.utils.kv['@0.1.16'].set({//set a new value of a key
key: `MuteCommand-Check`,//Key name
value: false,//New key value (in this case we are setting it to flase)
});
return 'Slash command created successfully';//if slash command is created successfully, it will return this
}
if (context.params.event.id === '0000000000000000')//checks if you are using `run` button
return 'Please run the slash command from Discord';//returns this
let user = await lib.discord.users['@0.2.0'].retrieve({//get info on a user
user_id: context.params.event.member.user.id,//user id (I am using context.params.event.member.user.id to get the info of the person who used the command)
});
let userId = context.params.event.data.options[0].value;//Gets user id from the first option (user)
let reason = context.params.event.data.options[1].value;//Gets reason from the second option (reason)
let roles = await lib.discord.guilds['@0.2.2'].roles.list({//lists the roles in a guild
guild_id: `${context.params.event.guild_id}`,//Guild id (I am using context.params.event.guild_id for the id of the guild where the command was used)
});
let hasPerm =
(context.params.event.member.permissions & (1 << 2)) === 1 << 2;//checks if the person has BAN_MEMBER permissions
if (hasPerm) {//if the user has permissions
if (!roles.find((r) => r.name === 'muted')?.id) {//if no role named muted is found
await lib.discord.channels['@0.3.0'].messages.create({//creates a message
channel_id: `${context.params.event.channel_id}`,//channel id (i am using context.params.event.channel_id to get the id of the channel where the command was used)
content: ``,//content of the message
embed: {//embed
type: 'rich',
title: `**Mute Role Not Found!**`,//embed title
description: `Mute Role named \`muted\` Not Found! Make a New role named \`muted\` and try running this command again!`,//embed description
color: 131644,//embed color
footer: {
text: `${user.username}#${user.discriminator}`,//this will display username and user discriminator of the person who used the command like: Nintendo_bot#7518
icon_url: `${user.avatar_url}`,//this will display the user icon of the user who used it
},
},
});
return;
}
try {
let muteRole = `${roles.find((r) => r.name === 'muted')?.id}`;//defining and searching for mute role
let user_roles = await lib.discord.guilds['@0.2.2'].members.retrieve({//getting info on the user
user_id: `${userId}`, // required
guild_id: `${context.params.event.guild_id}` // required
});
if (user_roles.roles.includes(muteRole)) {//if the roles of the user includes muted
await lib.discord.guilds['@0.2.2'].members.roles.destroy({//removes muted role
role_id: `${roles.find((r) => r.name === 'muted')?.id}`,//role id
user_id: `${userId}`,//user id
guild_id: `${context.params.event.guild_id}`//guild id
});
await lib.discord.channels['@0.2.0'].messages.create({//create a message
channel_id: `${context.params.event.channel_id}`,
content: ``,
embed: {
type: 'rich',
title: `**Member Unmuted!**`,
description: `
*Member:* **<@${userId}>**
*ID:* **${userId}**
*Moderator:* **<@${context.params.event.member.user.id}>**
*For:* **${reason}**`,
color: 131644,
},
});
await lib.discord.users['@0.2.0'].dms.create({//dm the user
recipient_id: `${userId}`,
content: ``,
embed: {
type: 'rich',
title: `**You were Unmuted!**`,
description: `
*Member:* **<@${userId}>**
*ID:* **${userId}**
*Moderator:* **<@${context.params.event.member.user.id}>**
*For:* **${reason}**`,
color: 131644,
},
});
} else{//if user does not have muted role
await lib.discord.guilds['@0.2.2'].members.roles.update({//add muted role to the user
role_id: `${roles.find((r) => r.name === 'muted')?.id}`,
user_id: `${userId}`,
guild_id: `${context.params.event.guild_id}`
})
await lib.discord.channels['@0.2.0'].messages.create({//send a message
channel_id: `${context.params.event.channel_id}`,
content: ``,
embed: {
type: 'rich',
title: `**Member Muted!**`,
description: `
*Member:* **<@${userId}>**
*ID:* **${userId}**
*Moderator:* **<@${context.params.event.member.user.id}>**
*For:* **${reason}**`,
color: 131644,
},
});
await lib.discord.users['@0.2.0'].dms.create({//dm the user
recipient_id: `${userId}`,
content: ``,
embed: {
type: 'rich',
title: `**You were Muted!**`,
description: `
*Member:* **<@${userId}>**
*ID:* **${userId}**
*Moderator:* **<@${context.params.event.member.user.id}>**
*For:* **${reason}**`,
color: 131644,
},
});
}
} catch (e) {//if there is an error
console.log(e)//log the error in console
await lib.discord.channels['@0.3.0'].messages.create({//create a message
channel_id: `${context.params.event.channel_id}`,//channel id, I am using context.params.event.channel_id to sent it where the command was triggered
content: ``,//content of the message
embed: {
type: 'rich',
title: `**The was an Error!**`,
description: `The error is: \n\`\`\`js\n${e}\`\`\``,
color: 131644,
footer: {
text: `${user.username}#${user.discriminator}`,//username and discriminator of the user who used it like Nintendo_bot#7518
icon_url: `${user.avatar_url}`,//user icon
},
},
});
}
}else {//if the user does not have permissions
await lib.discord.channels['@0.3.0'].messages.create({//create a message
channel_id: `${context.params.event.channel_id}`,//channel id, I am using context.params.event.channel_id to send the message where the command was used.
content: ``,//content of the message
embed: {
type: 'rich',
title: `**Error!**`,//title of the embed
description: `You don't have permission to use this!`,//embed description
color: 131644,//embed color
footer: {
text: `${user.username}#${user.discriminator}`,//username and discriminator of the user who used the command
icon_url: `${user.avatar_url}`,//icon of the user who used the command
},
},
});
}