为什么我会收到“类型错误:无法读取未定义的属性‘发送’”?

曼B1023

所以这是更早的工作,但是在添加我的 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我收到TypeError:无法读取未定义的属性“ now”

操作SDK会收到此错误TypeError:无法读取未定义的属性“输出”

为什么我会收到TypeError:即使我的Web元素引用正确,也无法读取未定义错误的属性“ getText”?

我不知道为什么我会收到以下消息:“ ERROR TypeError:无法设置未定义的属性” position“

无法读取未定义的push属性?即使我将评论推送到评论数组,为什么仍会收到此错误?

我为什么会收到此错误:Uncaught TypeError:无法读取未定义的属性“ john”

为什么会收到“构造函数未定义”错误?

为什么我会收到该错误调用未定义方法Facebook \ Facebook :: api()?

我在这行代码中收到“类型错误:无法读取未定义的属性”

为什么我会遇到未捕获的TypeError:无法在此处读取未定义的属性“样式”

为什么我会收到Uncaught TypeError:无法在分配时读取未定义的属性“ 0”?

类型错误:无法读取未定义的属性“发送”:NodeJs

为什么我会收到此错误“NameError:name 'self' 未定义。”

FirstFactorial Javascript - 为什么我会收到未定义的输出?

为什么我收到错误“未捕获的类型错误:无法读取 HTMLLIElement 处未定义的属性‘样式’。”?

错误类型错误:无论我尝试什么,都无法读取未定义的属性“路径”

我收到以下错误:错误类型错误:无法读取未定义的属性“无效”

为什么我会收到未捕获的类型错误:如果声明了 var,则无法读取未定义的属性“长度”?

为什么我会收到对“getche”的未定义引用错误?

为什么我会收到此“主页未定义无未定义”错误

我收到类型错误:无法读取未定义的属性“推送”。我该怎么办?

为什么我收到错误:类型错误:无法读取未定义的“拆分”的属性

NodeJS 和 MySQL Promise:为什么我会收到 ReferenceError:DataBase 未定义?

当我尝试使用 JavaScript 客户端连接到 Elasticsearch 时,为什么会收到错误“无法读取未定义的属性 'MAX_LENGTH'”?

Vuetify/Javascript 为什么我会收到 ReferenceError: 变量未定义

我不断收到类型错误:无法读取未定义的属性“缓存”

为什么我会收到 TypeError:无法读取未定义的 react-router-dom 的属性“push”

Vue/Vuetify:为什么我会收到一个 ReferenceError:变量未定义?

为什么我收到错误 - TypeError:无法读取未定义的属性“名称”