简体   繁体   English

discord.js guildCreate.js 事件未触发

[英]discord.js guildCreate.js event not firing

We're trying to get our guildCreate.js event trigger working for Corion.我们正在尝试让我们的guildCreate.js事件触发器为 Corion 工作。 But it's throwing the following error:但它抛出以下错误:

3|Corion   | (node:16154) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'cache' of undefined

Here is our code:这是我们的代码:

module.exports = async (client, guild) => {
    const channel = guild.channels.cache.find(channel => channel.type === 'text' && channel.permissionsFor(guild.me).has('SEND_MESSAGES'))
    console.log(`${client.name}` + `has entered` + `${guild.name}.`)
    channel.send(`Thanks for invite me!\n\nType **c!help** to see a full list of available commands!`).catch(console.error)
};

This is our events handler if it helps:如果有帮助,这是我们的事件处理程序:

const events = fs.readdirSync('./events').filter(file => file.endsWith('.js'));

for (const file of events) {
    console.log(`Loading discord.js event ${file}`);
    const event = require(`./events/${file}`);
    client.on(file.split(".")[0], event.bind(null, client));
};

We're running discord.js: 12.5.1我们正在运行 discord.js: 12.5.1

This won't work in this particular situation because of how the callback of client.on("guildCreate") works.由于client.on("guildCreate")的回调如何工作,这在这种特殊情况下不起作用。 In fact, you may find that this will not work properly for pretty much all of the different events of Discord because of how the callbacks work in combination with how you are using .bind() .实际上,您可能会发现这对于 Discord 的几乎所有不同事件都无法正常工作,因为回调如何与您使用.bind()的方式结合使用。

The Problem问题

Think about how client.on() works in terms of its callback.考虑一下client.on()在回调方面的工作方式。 When using client.on() you do something like this: client.on("guildCreate", callback) .使用client.on()时,您可以执行以下操作: client.on("guildCreate", callback) And this is what client.on() does when that event is triggered: it calls callback(guild) .这就是触发该事件时client.on()所做的:它调用callback(guild)

Now think about what you're doing in your code.现在想想你在代码中做了什么。 You're doing event.bind(null, client) on your event handler function, which is in the form callback(client, guild) .您正在事件处理程序 function 上执行event.bind(null, client) ,其形式为callback(client, guild) So this is what happens:所以这就是发生的事情:

  1. You set the value of client in your callback to your Discord.Client , via .bind() .您通过.bind()将回调中的client值设置为Discord.Client So now your client parameter is Discord.Client , and your guild parameter is undefined .所以现在你的client参数是Discord.Client ,你的guild参数是undefined
  2. When the event is triggered, it now sets your first parameter ( client ) to a Guild object.触发事件时,它现在将您的第一个参数( client )设置为Guild object。 So now your client parameter is Guild and your guild parameter is still undefined .所以现在你的client参数是Guild并且你的guild参数仍然是undefined
  3. Now in your callback, you try to do guild.channels.cache .现在在您的回调中,您尝试执行guild.channels.cache guild is still undefined, so guild.channels is undefined, and therefore you get the error: Cannot read property 'cache' of undefined . guild仍然未定义,因此guild.channels未定义,因此您会收到错误: Cannot read property 'cache' of undefined

The Solution解决方案

Alright, so how do you solve this issue?好的,那么你如何解决这个问题? Well, your first instinct might be to simply switch the order of your client and guild parameters in your callback, and then tweak your .bind() appropriately.好吧,您的第一直觉可能是简单地在回调中切换clientguild参数的顺序,然后适当地调整您的.bind() That might work for this particular event, but remember that some events have two or more parameters in their callbacks.这可能适用于这个特定事件,但请记住,某些事件在其回调中有两个或多个参数。 guildCreate only sends you a single guild argument, but something like guildUpdate would send you two (consequently causing the exact same error you are currently experiencing). guildCreate只向您发送一个公会参数,但像guildUpdate这样的东西会向您发送两个(因此导致您当前遇到的完全相同的错误)。

No matter what solution to this you pursue, you will need to scrap the client parameter altogether.无论您追求什么解决方案,您都需要完全废弃client参数。 If you want to account for any number of parameters and still use the convenient .bind() method, then instead of binding your client to the first parameter of the callback, you could instead bind your client to the this keyword.如果您想考虑任意数量的参数并仍然使用方便的.bind()方法,那么您可以将客户端绑定到this关键字,而不是将客户端绑定到回调的第一个参数。 Here's an example of what I mean by that:这是我的意思的一个例子:

Event handler:事件处理程序:

const events = fs.readdirSync('./events').filter(file => file.endsWith('.js'));

for (const file of events) {
    console.log(`Loading discord.js event ${file}`);
    const event = require(`./events/${file}`);
    client.on(file.split(".")[0], event.bind(client));
};

guildCreate.js:公会创建.js:

module.exports = async function (guild) {
    const client = this;
    const channel = guild.channels.cache.find(channel => channel.type === 'text' && channel.permissionsFor(guild.me).has('SEND_MESSAGES'))
    console.log(`${client.user.username}` + `has entered` + `${guild.name}.`)
    channel.send(`Thanks for invite me!\n\nType **c!help** to see a full list of available commands!`).catch(console.error)
};

This should ensure that client.on() doesn't mess with the value of client , and therefore fix the specific error you are experiencing.这应该确保client.on()不会与client的值混淆,从而修复您遇到的特定错误。

What Changed发生了什么变化

So overall, you need to: change .bind(null, client) to .bind(client) in your event handler, remove the client parameter from your guildCreate.js callback, use this to access your client, change client.name to client.user.username because the former is incorrect, and switch from ES6 arrow functions ( () => {} ) to the standard function syntax ( function() {} ) because of how the this keyword works within the former.因此,总的来说,您需要:在事件处理程序中将.bind(null, client)更改为.bind(client) ,从 guildCreate.js 回调中删除client参数,使用this来访问您的客户端,将client.name更改为client.user.username因为前者不正确,并从 ES6 箭头函数 ( () => {} ) 切换到标准 function 语法 ( function() {} ) 因为this关键字在前者中的工作方式。

I have not tested this solution, but it should hypothetically work to at least solve the error you are currently facing.我没有测试过这个解决方案,但假设它至少可以解决您当前面临的错误。 Let me know if it doesn't, or if there are other relevant errors in this code, and I will edit the answer to fix them.如果没有,或者此代码中是否存在其他相关错误,请告诉我,我将编辑答案以修复它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM