在 discord.js 上嵌入欢迎信息
Making a welcome message an embed on discord.js
我已将 MongoDB 连接到我的 discord.js 代码,并已将 setwelcome
命令作为每个服务器的数据,以便每个服务器都可以自定义自己的欢迎消息。一切都很好,我只是想知道是否有任何方法可以使消息显示为嵌入?这是代码:
//importing all the needed files and languages
const mongo = require('./mongo')
const command = require('./command')
const welcomeSchema = require('./schemas/welcome-schema')
const mongoose = require('mongoose')
const Discord = require('discord.js')
mongoose.set('useFindAndModify', false);
//my code is inside this export
module.exports = (client) => {
//this next line is for later
const cache = {}
command(client, 'setwelcome', async (message) => {
const { member, channel, content, guild } = message
//checking to see that only admins can do this
if (!member.hasPermissions === 'ADMINISTRATOR') {
channel.send('You do not have the permission to run this command')
return
}
//simplifying commands
let text = content
//this is to store just the command and not the prefix in mongo compass
const split = text.split(' ')
if (split.length < 2) {
channel.send('Please provide a welcome message!')
return
}
split.shift()
text = split.join(' ')
//this is to not fetch from the database after code ran once
cache[guild.id] = [channel.id, text]
//this is to store the code inside mongo compass
await mongo().then(async (mongoose) => {
try {
await welcomeSchema.findOneAndUpdate({
_id: guild.id
}, {
_id: guild.id,
channelId: channel.id,
text,
}, {
upsert: true
})
} finally {
mongoose.connection.close()
}
})
})
//this is to fetch from the database
const onJoin = async (member) => {
const { guild } = member
let data = cache[guild.id]
if (!data) {
console.log('FETCHING FROM DATABASE')
await mongo().then( async (mongoose) => {
try {
const result = await welcomeSchema.findOne({ _id: guild.id })
cache[guild.id] = data = [result.channelId, result.text]
} finally {
mongoose.connection.close()
}
})
}
//this is to simplify into variables
const channelId = data[0]
const text = data[1]
/*this is where the message sends on discord. the second of these 2 lines is what I want embedded
which is basically the welcome message itself*/
const channel = guild.channels.cache.get(channelId)
channel.send(text.replace(/<@>/g, `<@${member.id}>`))
}
//this is to test the command
command(client, 'simjoin', message => {
onJoin(message.member)
})
//this is so the command works when someone joins
client.on('guildMemberAdd', member => {
onJoin(member)
})
}
我知道通常如何制作嵌入,但我现在只是对嵌入的内容感到困惑 .setDescription()
。
请指教
如果您只想将消息作为嵌入发送,请创建一个 MessageEmbed
并使用 setDescription()
并将描述作为唯一参数。然后用 channel.send(embed)
.
发送
const embed = new Discord.MessageEmbed();
embed.setDescription(text.replace(/<@>/g, `<@${member.id}>`));
channel.send(embed);
顺便说一句,如果你对具体方法的使用感到困惑,你可以随时在官方discord.js documentation上搜索方法名称,这样你就不必在这里等待答案了。祝你创建机器人好运!
我已将 MongoDB 连接到我的 discord.js 代码,并已将 setwelcome
命令作为每个服务器的数据,以便每个服务器都可以自定义自己的欢迎消息。一切都很好,我只是想知道是否有任何方法可以使消息显示为嵌入?这是代码:
//importing all the needed files and languages
const mongo = require('./mongo')
const command = require('./command')
const welcomeSchema = require('./schemas/welcome-schema')
const mongoose = require('mongoose')
const Discord = require('discord.js')
mongoose.set('useFindAndModify', false);
//my code is inside this export
module.exports = (client) => {
//this next line is for later
const cache = {}
command(client, 'setwelcome', async (message) => {
const { member, channel, content, guild } = message
//checking to see that only admins can do this
if (!member.hasPermissions === 'ADMINISTRATOR') {
channel.send('You do not have the permission to run this command')
return
}
//simplifying commands
let text = content
//this is to store just the command and not the prefix in mongo compass
const split = text.split(' ')
if (split.length < 2) {
channel.send('Please provide a welcome message!')
return
}
split.shift()
text = split.join(' ')
//this is to not fetch from the database after code ran once
cache[guild.id] = [channel.id, text]
//this is to store the code inside mongo compass
await mongo().then(async (mongoose) => {
try {
await welcomeSchema.findOneAndUpdate({
_id: guild.id
}, {
_id: guild.id,
channelId: channel.id,
text,
}, {
upsert: true
})
} finally {
mongoose.connection.close()
}
})
})
//this is to fetch from the database
const onJoin = async (member) => {
const { guild } = member
let data = cache[guild.id]
if (!data) {
console.log('FETCHING FROM DATABASE')
await mongo().then( async (mongoose) => {
try {
const result = await welcomeSchema.findOne({ _id: guild.id })
cache[guild.id] = data = [result.channelId, result.text]
} finally {
mongoose.connection.close()
}
})
}
//this is to simplify into variables
const channelId = data[0]
const text = data[1]
/*this is where the message sends on discord. the second of these 2 lines is what I want embedded
which is basically the welcome message itself*/
const channel = guild.channels.cache.get(channelId)
channel.send(text.replace(/<@>/g, `<@${member.id}>`))
}
//this is to test the command
command(client, 'simjoin', message => {
onJoin(message.member)
})
//this is so the command works when someone joins
client.on('guildMemberAdd', member => {
onJoin(member)
})
}
我知道通常如何制作嵌入,但我现在只是对嵌入的内容感到困惑 .setDescription()
。
请指教
如果您只想将消息作为嵌入发送,请创建一个 MessageEmbed
并使用 setDescription()
并将描述作为唯一参数。然后用 channel.send(embed)
.
const embed = new Discord.MessageEmbed();
embed.setDescription(text.replace(/<@>/g, `<@${member.id}>`));
channel.send(embed);
顺便说一句,如果你对具体方法的使用感到困惑,你可以随时在官方discord.js documentation上搜索方法名称,这样你就不必在这里等待答案了。祝你创建机器人好运!