【问题标题】:Why am I getting a "TypeError: Cannot read property 'send' of undefined"?为什么我会收到“TypeError:无法读取未定义的属性‘发送’”?
【发布时间】:2021-10-28 08:10:49
【问题描述】:

所以这在早些时候工作,但在添加我的 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);
});

【问题讨论】:

    标签: discord discord.js


    【解决方案1】:

    这是因为所有者不在机器人的缓存中。你应该使用UserManager.fetch(在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'
            }]
          })
    

    【讨论】:

      猜你喜欢
      • 2020-06-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-17
      • 2021-11-02
      相关资源
      最近更新 更多