所以这是更早的工作,但是在添加我的 guildCache 映射以保存服务器特定变量后,它停止工作。我不确定错误发生在哪里,所以我将发送我的所有代码,直到错误点。基本上,在我的 guildCreate 事件中,机器人通常会向服务器所有者发送图像和文本,但突然间我的工作代码现在被破坏了。有什么想法吗?
我的意图是 GUILDS、GUILD_MESSAGES、DIRECT_MESSAGES
// Require the necessary modules
const config = require('./Data/config.json');
const insulter = require('insult');
require('dotenv').config();
const { Client, Collection } = require('discord.js');
// Create a new client instance
const client = new Client({ intents: 4609 }); // GUILDS, GUILD_MESSAGES, DIRECT_MESSAGES
const guildCache = new Collection(); //For saving independent server variables
// Functions
function getUserFromMention(mention) {
if (!mention) return;
if (mention.startsWith('<@') && mention.endsWith('>')) {
mention = mention.slice(2, -1);
if (mention.startsWith('!')) {
mention = mention.slice(1);
}
return client.users.cache.get(mention);
}
}
// --------------- BOT EVENTS -----------------
client.once('ready', () => {
console.log(`[Client] Logged in as ${client.user.tag}!`);
client.user.setActivity('you cry.', {
type: 'WATCHING'
});
// Map servers to cache for variables (also done on server join and leave)
client.guilds.cache.forEach(guild => {
guildCache.set(guild.id, {
bullyTarget: undefined,
lastInsultGenerated: undefined
});
});
console.log(guildCache);
console.log('');
});;
// On join server
client.on('guildCreate', async (guild) => {
console.log(`[Client]: Joined \"${guild.name}\"!`);
guildCache.set(guild.id, {
bullyTarget: undefined,
lastInsultGenerated: undefined
});
console.log(guildCache);
console.log('');
await client.users.cache.get(guild.ownerId).send({
content: 'You\'ll ***regret*** adding me.',
files: [{
attachment: 'src/Data/trollge.jpg',
name: 'trollge.jpg'
}]
})
.then(console.log('[Client]: Sent join message to server owner :D\n'))
.catch(console.error);
});
这是因为所有者不在机器人的缓存中。你应该使用UserManager.fetch
(on client.users
)
(await client.users.fetch(guild.ownerId)).send({
content: 'You\'ll ***regret*** adding me.',
files: [{
attachment: 'src/Data/trollge.jpg',
name: 'trollge.jpg'
}]
})
您还可以使用该Guild.fetchOwner()
方法访问他们的GuildMember
信息(角色、昵称等)
const owner = await guild.fetchOwner()
owner.send({
content: 'You\'ll ***regret*** adding me.',
files: [{
attachment: 'src/Data/trollge.jpg',
name: 'trollge.jpg'
}]
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句