this is my ../Events/Guild/guildMemberAdd.js https://sourceb.in/iEEfLj7uM7
im trying to set placeholders that will in turn give out an output like
Welcome to OnlyScoped.gg #azz#5271! We're glad to have you as the 500th member.
but output is
Welcome to OnlyScoped.gg <#undefined>! We're glad to have you join us as the undefinedth member.`
../Commands/Moderation/setup-welcome.js
const {Message, Client, SlashCommandBuilder, PermissionFlagsBits} = require("discord.js");
const welcomeSchema = require("../../Models/Welcome");
const {model, Schema} = require("mongoose");
module.exports = {
data: new SlashCommandBuilder()
.setName("setup-welcome")
.setDescription("Set up your welcome message for the discord bot.")
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
.addChannelOption(option =>
option.setName("channel")
.setDescription("Channel for welcome messages.")
.setRequired(true)
)
.addStringOption(option =>
option.setName("welcome-message")
.setDescription("Enter your welcome message.")
.setRequired(true)
)
.addRoleOption(option =>
option.setName("welcome-role")
.setDescription("Enter your welcome role.")
.setRequired(true)
),
async execute(interaction) {
const {channel, options} = interaction;
const welcomeChannel = options.getChannel("channel");
const welcomeMessage = options.getString("welcome-message");
const roleId = options.getRole("welcome-role");
if(!interaction.guild.members.me.permissions.has(PermissionFlagsBits.SendMessages)) {
interaction.reply({content: "I don't have permissions for this.", ephemeral: true});
}
welcomeSchema.findOne({Guild: interaction.guild.id}, async (err, data) => {
if(!data) {
const newWelcome = await welcomeSchema.create({
Guild: interaction.guild.id,
Channel: welcomeChannel.id,
Msg: welcomeMessage,
Role: roleId.id
});
}
interaction.reply({content: 'Succesfully created a welcome message', ephemeral: true});
})
}
}
../Models/Welcome.js
const { model, Schema } = require("mongoose");
let welcomeSchema = new Schema({
Guild: String,
Channel: String,
Msg: String,
Role: String,
});
module.exports = model("Welcome", welcomeSchema);
im attempting to use string.replace()but its not working as expected
i decided to put it in guildMemberAdd.js since when a member joins this gets runs so it would be unwise to place it in setup-welcome.js or Welcome.js since those are not listening for anything.
for reference here's my package.json:
https://sourceb.in/FMBgygjyoh
for the record i cant find any of the id's like member.id or member.count so those are wild guesses as to what they are. it could very well just be that as im still learning v14 this is my first project in it.
one other way i thought could work is if i just pass it off as an interpolated string in mongodb but it seems that the only string is with "" so i cant use default ones like ${member.count} so i decided to add placeholders
The basics of formatting a template are this:
const string = "Welcome to OnlyScoped.gg {tagUser}! We're glad to have you as the {memberCount} member.";
string = string.replace(/{tagUser}/g, member.toString());
string = string.replace(/{memberCount}/g, '500th');
return string; // "Welcome to OnlyScoped.gg <#123456789012345678>! We're glad to have you as the 500th member.";
To make something extensible, put template strings like this somewhere in your configuration:
{
"welcome_message": "Welcome to OnlyScoped.gg {tagUser}! We're glad to have you as the {ordinal:memberCount} member."
}
and make a function
function formatMessage(template, lookup) {
let output = template;
output = output.replace(/{ordinal:([^}]*)}/g, (_, target) => withOrdinalSuffix(lookup(target)));
output = output.replace(/{([^}]*)}/g, (_, target) => lookup(target));
return output;
}
// https://stackoverflow.com/a/31615643/3310334
// turn 1 into '1st', 500 into '500th', 502 into '502nd'
function withOrdinalSuffix(n) {
var s = ["th", "st", "nd", "rd"],
v = n % 100;
return n + (s[(v - 20) % 10] || s[v] || s[0]);
}
and then use the function with a template and the lookup function:
client.on('guildMemberAdd', member => {
const welcomeMessageTemplate = config.welcome_message;
const memberCount = member.guild.members.filter(member => !member.user.bot).size;
const lookup = (item) => {
const items = {
memberCount,
tagUser: member.toString()
};
return items[item];
};
const welcomeMessage = formatMessage(welcomeMessageTemplate, lookup);
const welcomeChannel = member.guild.channels.cache.find(channel => channel.name === 'welcome');
welcomeChannel.send(welcomeMessage);
});
The main issue I can see is incorrect property names as you mention in the question.
DiscordJS Docs: GuildMember
member.id => The Members ID
member.user.username => The Members username
member.guild.name => The Server's Name
member.guild.memberCount => Number of users within the Server
I'd advise the user to input data in a specific format like Hello {userName}!. Then you could run a program like
while (string.includes('{userName}')) {
string = string.replace('{userName}', member.user.username);
}
Related
I've been programming a bot that manages points. I have a command with subcommands that allow to either add or remove users' points.
The question that I've not been able to solve is whether I can add/remove the points to many users with 1 command.
Example: I want to remove 25 points to 5 users. Instead of having to call the same command 5 times, I'd like to just do it one time.
Here is the code for this command:
const {
SlashCommandBuilder,
EmbedBuilder,
PermissionFlagsBits,
} = require("discord.js");
const { User } = require("../../schemas/user");
module.exports = {
data: new SlashCommandBuilder()
.setName("points")
.setDescription("Manage users' points.")
.setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages)
.addSubcommand((subcommand) =>
subcommand
.setName("add")
.setDescription("Adds points to the user.")
.addUserOption((option) =>
option
.setName("user")
.setDescription("User to whom the points should be added.")
.setRequired(true)
)
.addNumberOption((option) =>
option
.setName("value")
.setDescription("Value to be added.")
.setRequired(true)
.setMinValue(1)
)
)
.addSubcommand((subcommand) =>
subcommand
.setName("remove")
.setDescription("Removes points to the user.")
.addUserOption((option) =>
option
.setName("user")
.setDescription("User to whom the points should be removed.")
.setRequired(true)
)
.addNumberOption((option) =>
option
.setName("value")
.setDescription("Value to be removed.")
.setRequired(true)
)
),
async execute(interaction) {
const user = interaction.options.getUser("user");
const member = interaction.guild.members.cache.get(user.id);
const nickname = member.nickname;
const value = interaction.options.getNumber("value");
(userData =
(await User.findOne({ id: user.id })) || new User({ id: user.id })),
(embed = new EmbedBuilder().setColor("Random"));
if (nickname) {
userData.name = nickname;
} else {
userData.name = user.tag;
}
if (interaction.options.getSubcommand() === "add") {
userData.pointsA += value;
userData.pointsB += value;
userData.pointsMonth += value;
userData.save();
return interaction.reply({
embeds: [
embed.setDescription(`🟩 You added \` ${value}\` Points to ${user}.`),
],
});
} else if (interaction.options.getSubcommand() === "remove") {
userData.pointsA -= value;
userData.save();
return interaction.reply({
embeds: [
embed.setDescription(
`🟥 You removed \` ${value}\` Points to ${user}.`
),
],
});
}
},
};
You could either add 5 user options (require only one of them, so you can also only give the points to one user if you want) like user1, user2, user3, user4 and user5 and define them like that in the execution aswell.
Or you make the user option a string and you can enter the userIds, tags, or usernames seperated by commas and then make an array out of the users like this (i chose userIds):
const users = interaction.options.getString('user').split(',');
users.forEach(resolvable => {
const member = interaction.guild.members.cache.get(resolvable) || interaction.guild.members.cache.get(resolvable.slice(2, -1)) || interaction.guild.members.cache.get(resolvable.slice(3, -1);
if(!member) return;
// and then execute all the code you want to execute per user...
}
And btw you don't have to get the GuildMember like this:
const member = interaction.guild.members.cache.get(user.id);
You can get it like the User:
const member = interaction.options.getMember('<your user option name>');
This is my code and im getting this error: RangeError [EMBED_DESCRIPTION]: MessageEmbed description must be a string can can someone help me please? i want to make leaderboard
const { MessageEmbed } = require("discord.js");
const Discord = require('discord.js');
const Database = require("#replit/database");
const db = new Database();
module.exports = {
name: "Leaderboard",
aliases: ['leader', 'leaderboard', 'топ', 'Топ'],
run: async (client, message, args) => {
const collection = new Collection();
await Promise.all(
message.guild.members.cache.map(async (member) => {
const id = member.id;
const bal = await db.get(`balance_${id}`);
console.log(`${member.user.tag} -> ${bal}`);
return bal !== 0
? collection.set(id, {
id,
bal,
})
: null;
})
);
const data = collection.sort((a, b) => b.bal - a.bal).first(10);
message.channel.send(
new MessageEmbed()
.setTitle(`Leaderboard in ${message.guild.name}`)
.setDescription(
data.map((v, i) => {
return `${i + 1}) ${client.users.cache.get(v.id).tag} => **${v.bal} coins**`;
})
)
);
},
};```
In setDescription you're calling data.map(...) which returns an array. You can use Array.prototype.join to create a string from the array elements: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
data.map(...).join('\n')
If your array looked like this ['abc', 'def', 'ghi'] then the output of the join would be a string that looks like this:
abc
def
ghi
I'm setting up Slash Commands with my Discord.JS bot.
I have a /rank command in the XP/leveling portion of my bot, but when I check interaction.member.presence for displaying it in the rank card, it returns null.
I've tried to look up this problem and look through the documentation of Discord.JS, but nobody else seems to have had this problem yet and I don't see anything in the documentation or Discord.JS guide to help solve this problem.
Here is the /rank command:
const Discord = require("discord.js");
const SQLite = require("better-sqlite3");
const sql = new SQLite('./mainDB.sqlite')
const client = new Discord.Client({ intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES] });
const canvacord = require("canvacord");
module.exports = {
name: 'rank',
aliases: ['rank'],
description: "Get your rank or another member's rank",
cooldown: 3,
category: "Leveling",
async execute(interaction) {
if(!interaction.isCommand()) return console.log("yes");
await interaction.deferReply()
.then(console.log("a"))
.catch(console.error);
let user = interaction.user;
client.getScore = sql.prepare("SELECT * FROM levels WHERE user = ? AND guild = ?");
client.setScore = sql.prepare("INSERT OR REPLACE INTO levels (id, user, guild, xp, level, totalXP) VALUES (#id, #user, #guild, #xp, #level, #totalXP);");
const top10 = sql.prepare("SELECT * FROM levels WHERE guild = ? ORDER BY totalXP").all(interaction.guild.id);
let score = client.getScore.get(user.id, interaction.guild.id);
if (!score) {
return interaction.editReply(`This user does not have any XP yet!`)
}
const levelInfo = score.level
const nextXP = levelInfo * 2 * 250 + 250
const xpInfo = score.xp;
const totalXP = score.totalXP
let rank = top10.sort((a, b) => {
return b.totalXP - a.totalXP
});
let ranking = rank.map(x => x.totalXP).indexOf(totalXP) + 1
//if (!interaction.guild.me.hasPermission("ATTACH_FILES")) return interaction.editReply(`**Missing Permission**: ATTACH_FILES or MESSAGE ATTACHMENTS`);
try {
var cardBg = sql.prepare("SELECT bg FROM background WHERE user = ? AND guild = ?").get(user.id, message.guild.id).bg;
var bgType = "IMAGE";
} catch (e) {
var cardBg = "#000000";
var bgType = "COLOR";
}
console.log(interaction.member.presence);
const rankCard = new canvacord.Rank()
.setAvatar(user.displayAvatarURL({
format: "jpg"
}))
.setStatus(interaction.member.presence.status, true, 1)
.setCurrentXP(xpInfo)
.setRequiredXP(nextXP)
.setProgressBar("#5AC0DE", "COLOR")
.setUsername(user.username)
.setDiscriminator(user.discriminator)
.setRank(ranking)
.setLevel(levelInfo)
.setLevelColor("#5AC0DE")
.renderEmojis(true)
.setBackground(bgType, cardBg);
rankCard.build()
.then(data => {
const attachment = new Discord.MessageAttachment(data, "RankCard.png");
return interaction.editReply({attachments: [attachment]});
});
}
}
Notes:
I do have the Presence Intent enabled.
Sorry if this seems like too little information. It's just what I
know so far, and I can't think of anything that I can do about it.
I know this command is very messy. I'm not asking how to fix that. I will fix that later.
Even though you have the PRESENCE INTENT enabled, you need to specify that you will be using the aforementioned intent in ClientOptions.
const client = new Discord.Client({
intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES, Discord.Intents.FLAGS.GUILD_PRESENCES],
});
First You need to enable intent
const client = new Discord.Client({
intents: [
Discord.Intents.FLAGS.GUILD_PRESENCES
],
});
Now we need Create a var of member
let member = message.mentions.members.first()
For get the presence of members use #presence
console.log(member.presence)
Will get results as
Presence {
userId: 'USER_ID',
guild: [Guild],
status: 'online',
activities: [Array],
clientStatus: [Object]
}
I want get info about reactions but i can't. I need get reaction and authors this reactions from old message without listeners or something.
This code make something similar https://gist.github.com/acollierr17/c9e7aaf9eba97d8659b59395b5f2046d but don't work for me, var is empty. But i can get message m.channel.fetchMessage(ID)
I don't understand why this does not work, although the message is received to var. How collect all reactions and their authors of message to array?
I try this and few other ways.
if(calcreact == 1 && msg.content.startsWith('/calcreact')) { //start
calcreact = 0; // защита от флуда
let msgid = finddata(msg.content);
//let channel = 709517297664131082;
msg.channel.fetchMessage(msgid)
.then(m => {
//console.log('Message:', message.content);
let reactions = m.reactions;
let reaction = reactions.first();
let users = reaction.users.map((u) => u.toString());
console.log(users);
//console.log(m.reactions.fetch());
//console.log(m.reactions.forEach((reaction) => {}));
m.reactions.forEach((reaction) => console.log(reaction));
m.reactions.forEach((reaction) => console.log(reaction.users));
m.reactions.forEach((reaction) => console.log(reaction.users.map((u) => u.toString())) );
//console.log('Reactions:', );
});
setTimeout(antiflood, 500, 'calcreact');
} //end
From m.reactions.forEach((reaction) => console.log(reaction)); i can get huge info "array" and in marked current reaction(It seems to me https://i.imgur.com/OazszNR.png) but deep (users) empty. This is horrible...
The function bellow groups reactions by users, like this:
{
userId1: ['reactionId1', 'reactionId2'],
userId2: ['reactionId1', 'reactionId2'],
...
}
You can adapt this to get the users and reactions from messages, or any context you like.
OBS: It was written in TypeScript, but if you remove the typing, it turns into JavaScript.
Function:
import { MessageReaction } from "discord.js";
interface IReactionsByUsers {
[userId: string]: Array<string>;
}
DiscordClient.on("messageReactionAdd", (reaction: MessageReaction) => {
const reducer = (acc: IReactionsByUsers, cur: [string, MessageReaction]) => {
const [reactionId, reaction] = cur;
const usersIds = [...reaction.users.cache].map(([userId]) => userId);
for (const userId of usersIds) {
if (acc[userId]) {
acc[userId].push(reactionId);
} else {
acc[userId] = [reactionId];
}
}
return acc;
};
const messageReactions = [...reaction.message.reactions.cache];
const reactionByUsers = messageReactions.reduce(
reducer,
{} as IReactionsByUsers
);
return reactionByUsers;
});
use the client.on("messageReactionAdd" method and save the reactions in an array.
Im trying to develop a little warn system for my Discord Bot. If someone types !warn #mention Reason, it should store the data in a JSON File. It works, but only with one User in one Guild. What I want is, that
the JSON File looks like this:
{
"superniceguildid":
{
"member": "636302212787601408",
"warns": 3
},
{
"meber": "7837439745387549"
"warns": 1
}
}
Now only this exists:
{
"627818561947041816": {
"guild": "636302212787601408",
"warns": 3
},
}
How can I do it, that the File is generating like above?
My current code is this:
module.exports = {
name: 'warn',
description: "test",
execute(message, args){
const { Client, MessageEmbed } = require("discord.js")
const client = new Client()
const fs = require("fs")
const ms = require("ms")
warns = JSON.parse(fs.readFileSync("./warns.json", "utf8"))
client.servers = require ("./servers.json")
let guild = client.servers[message.guild.id].message
/*Embeds*/
const oops = new MessageEmbed()
.setTitle("Error")
.setColor("RED")
.setDescription("You cant warn a member. Please ask a Moderator")
.setAuthor("MemeBot", "this is a link")
const Mod = new MessageEmbed()
.setTitle("Error")
.setColor("RED")
.setDescription("You cant warn a Moderator.")
.setAuthor("MemeBot", "linkhere xD")
/**Commands */
let wUser = message.mentions.users.first() || message.guild.members.cache.fetch(`${args[0]}`)
if (!wUser) return message.channel.send("Are you sure, that this was a User? I think it wasn't one...")
let wReason = args.join(" ").slice(27)
if (!wReason) return message.channel.send("Please tell me, why you want to warn this person. Because, you know, it's a warn :D");
if(!message.member.hasPermission("KICK_MEMBERS")) return message.channel.send(oops)
if(wUser.hasPermission("KICK_MEMBERS")) return message.channel.send(Mod)
if(!warns[message.guild.id]) warns[message.guild.id] = {
user: wUser.id,
warns: 0
}
warns[wUser.id].warns++
fs.writeFile("./warns.json", JSON.stringify(warns, null, 4), err => {
if(err) console.log(err)
});
let warnEmbed = new MessageEmbed()
.setTitle("Warned")
.setColor("YELLOW")
.addField("Warned User", `${wUser}`)
.addField("Moderator", `${message.author.id}`)
.addField("Reason", `${wReason}`)
.addField("Number of Warnings", warns[wUser.id].warns)
.addField("Warned at", `${message.createdAt}`)
let warnonEmbed = new MessageEmbed()
.setTitle("Warned")
.setColor("YELLOW")
.addField("Warned on", `${message.guild.name}`)
.addField("Moderator", `${message.author}`)
.addField("Reason", `${wReason}`)
.addField("Warned at", `${message.createdAt}`)
let logchannel = message.guild.channels.cache.find(c => c.id === 'id');
if(!logchannel) return
wUser.send(warnonEmbed)
logchannel.send(warnEmbed)
}
}
That particular layout doesn't make a lot of hierarchical sense. You might want to nest the user inside the guild and any parameters belonging to the user inside that. Something like this...
"superniceguildid":
{
"636302212787601408":
{
"warns": 3
},
"7837439745387549":
{
"warns": 1
}
},
Accessing it then would be as easy as using something like the following:
let guildWarns = warns["superniceguildid"];
let userWarns = guildWarns["636302212787601408"];
let numberOfWarns = userWarns.warns;
you can combine that as well.
let numberOfWarns = warns["superniceguildid"]["636302212787601408"].warns;
Of course, remember that if it doesn't exist it will be undefined.