未定义公会成员
guildMember is not defined
我试图在有人访问我的服务器时发出欢迎消息,但我正在尝试这样做,它需要从另一个文件嵌入。它说 guildMember 未定义,我理解,但我不知道如何定义它。
请注意,我使用的是 discord.js 版本 12。
这是我的 index.js:
const express = require('express');
const app = express();
const port = 8080;
app.get('/', (req, res) => res.send('Hello World!'));
app.listen(port, () => console.log('Hosted site'));
const { Client, Intents } = require('discord.js');
const Discord = require('discord.js');
const variable = require('./commands/variable');
const welcome = require('./commands/welcome');
const mongoose = require('mongoose');
mongoose.connect(process.env['mongo'], { useNewUrlParser: true, useUnifiedTopology: true }).then(console.log('Mongo is connected!'))
const fs = require('fs');
const prefix = ('m?');
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS],
partials: ['MESSAGE', 'CHANNEL', 'REACTION'],
});
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(File => File.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command)
}
client.once('ready', async () => {
console.log('MBot is ready!');
client.user.setActivity("Vedad", {
type: "LISTENING"
});
});
client.on('guildMemberAdd', guildMember => {
guildMember.guild.channels.cache.get('975033510186324069').send(welcome.wlc)
})
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'wlcmsgid') {
client.commands.get('wlcmsgid').execute(message, args, Discord, client);}
else if (command === 'ping') {
client.commands.get('ping').execute(message, args, Discord, client);}
})
client.login(process.env['token']);
这里是 welcome.js 文件:
const Discord = require('discord.js');
const variable = require('./variable');
module.exports.wlc = new Discord.MessageEmbed()
.setColor('#000000')
.setDescription(`━━━━━━━━━━━━━━━━━━━━━━━━
Welcome <@${guildMember.user.id}> to **GhostCommunity!**
━━━━━━━━━━━━━━━━━━━━━━━━`)
这是我的控制台错误:
ReferenceError: guildMember is not defined
at Object.<anonymous> (/home/runner/MBot-1/commands/welcome.js:8:21)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/home/runner/MBot-1/index.js:10:17)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
我只是不知道如何在 welcome.js 中定义 guildMember。请帮忙,谢谢
您可以在 guildMemberAdd
事件处理程序中访问 guildMember
,因此您应该将 welcome.wlc
更改为 returns 一个 Discord.MessageEmbed()
并获取guildMember 作为参数。
welcome.js
module.exports.wlc = (member) => {
return new Discord.MessageEmbed().setColor('#000000')
.setDescription(`━━━━━━━━━━━━━━━━━━━━━━━━
Welcome <@${member.user.id}> to **GhostCommunity!**
━━━━━━━━━━━━━━━━━━━━━━━━`)
}
index.js
client.on('guildMemberAdd', guildMember => {
guildMember.guild.channels.cache.get('975033510186324069').send(welcome.wlc(guildMember))
})
我试图在有人访问我的服务器时发出欢迎消息,但我正在尝试这样做,它需要从另一个文件嵌入。它说 guildMember 未定义,我理解,但我不知道如何定义它。
请注意,我使用的是 discord.js 版本 12。
这是我的 index.js:
const express = require('express');
const app = express();
const port = 8080;
app.get('/', (req, res) => res.send('Hello World!'));
app.listen(port, () => console.log('Hosted site'));
const { Client, Intents } = require('discord.js');
const Discord = require('discord.js');
const variable = require('./commands/variable');
const welcome = require('./commands/welcome');
const mongoose = require('mongoose');
mongoose.connect(process.env['mongo'], { useNewUrlParser: true, useUnifiedTopology: true }).then(console.log('Mongo is connected!'))
const fs = require('fs');
const prefix = ('m?');
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS],
partials: ['MESSAGE', 'CHANNEL', 'REACTION'],
});
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(File => File.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command)
}
client.once('ready', async () => {
console.log('MBot is ready!');
client.user.setActivity("Vedad", {
type: "LISTENING"
});
});
client.on('guildMemberAdd', guildMember => {
guildMember.guild.channels.cache.get('975033510186324069').send(welcome.wlc)
})
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'wlcmsgid') {
client.commands.get('wlcmsgid').execute(message, args, Discord, client);}
else if (command === 'ping') {
client.commands.get('ping').execute(message, args, Discord, client);}
})
client.login(process.env['token']);
这里是 welcome.js 文件:
const Discord = require('discord.js');
const variable = require('./variable');
module.exports.wlc = new Discord.MessageEmbed()
.setColor('#000000')
.setDescription(`━━━━━━━━━━━━━━━━━━━━━━━━
Welcome <@${guildMember.user.id}> to **GhostCommunity!**
━━━━━━━━━━━━━━━━━━━━━━━━`)
这是我的控制台错误:
ReferenceError: guildMember is not defined
at Object.<anonymous> (/home/runner/MBot-1/commands/welcome.js:8:21)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/home/runner/MBot-1/index.js:10:17)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
我只是不知道如何在 welcome.js 中定义 guildMember。请帮忙,谢谢
您可以在 guildMemberAdd
事件处理程序中访问 guildMember
,因此您应该将 welcome.wlc
更改为 returns 一个 Discord.MessageEmbed()
并获取guildMember 作为参数。
welcome.js
module.exports.wlc = (member) => {
return new Discord.MessageEmbed().setColor('#000000')
.setDescription(`━━━━━━━━━━━━━━━━━━━━━━━━
Welcome <@${member.user.id}> to **GhostCommunity!**
━━━━━━━━━━━━━━━━━━━━━━━━`)
}
index.js
client.on('guildMemberAdd', guildMember => {
guildMember.guild.channels.cache.get('975033510186324069').send(welcome.wlc(guildMember))
})