DM every user in a server that has a role. User the command /dm and all of the users with the given role will be sent the message. Run the code by clicking the green run button to create the command in your server
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
//Create the slash command
let check = await lib.utils.kv['@0.1.16'].get({
key: `DM_COMMAND_CREATE`,
defaultValue: false
});
if (check === false) {
await lib.discord.commands['@0.1.1'].create({
guild_id: process.env.COMMAND_GUILD_ID,
name: `dm`,
description: `DM all users in a server that have a role`,
options: [{
type: 8,
name: `role`,
description: `Role you want to dm`,
required: true
},
{
type: 3,
name: `message`,
description: `Message to send to the users`,
required: true
}]
});
await lib.utils.kv['@0.1.16'].set({
key: `DM_COMMAND_CREATE`,
value: true
});
console.log(`Command has been created! Run the command /dm!`);
} else if (check === true) {
console.log(`Command has been created! Run the command /dm!`);
try {
//Sends the message
let role = context.params.event.data.options[0].value
let message = context.params.event.data.options[1].value
let user = await lib.discord.guilds['@0.2.4'].members.list({
guild_id: context.params.event.guild_id,
limit: 1000,
});
for (let i = 0; i < user.length; i++) {
if (user[i].roles.includes(role)) {
await lib.discord.users['@0.2.1'].dms.create({
recipient_id: user[i].user.id,
content: message,
});
}
}
} catch (e) {
console.log(`Make sure you use the command from Discord!`);
}
}