英文:
Creating a Scheduled Event in Discord | discord.js
问题
由于Discord最近在其API中添加了GuildScheduledEvent
,我想制作一个可以创建事件的机器人(在SlashCommand上),但我无法使整个GuildScheduledEvent
和GuildScheduledEventManager
正常工作。
我一直收到这样的错误:
TypeError: Cannot read properties of undefined (reading 'options')
at new CachedManager (C:\dev\Discord Bots\node_modules\discord.js\src\managers\CachedManager.js:21:64)
at new GuildScheduledEventManager (C:\dev\Discord Bots\node_modules\discord.js\src\managers\GuildScheduledEventManager.js:17:5)
at Object.execute (C:\dev\Discord Bots\better bot\bot-data\commands\cancel_film.js:11:31)
at Client.<anonymous> (C:\dev\Discord Bots\better bot\main.js:44:17)
at Client.emit (node:events:513:28)
at InteractionCreateAction.handle (C:\dev\Discord Bots\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
at module.exports [as INTERACTION_CREATE] (C:\dev\Discord Bots\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (C:\dev\Discord Bots\node_modules\discord.js\src\client\websocket\WebSocketManager.js:354:31)
at WebSocketManager.<anonymous> (C:\dev\Discord Bots\node_modules\discord.js\src\client\websocket\WebSocketManager.js:238:12)
at WebSocketManager.emit (C:\dev\Discord Bots\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
我知道它出错的地方,但不明白为什么。
这是来自SlashCommand的代码:
const { SlashCommandBuilder, GuildScheduledEventManager, GuildScheduledEventPrivacyLevel, GuildScheduledEventEntityType } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('test_command')
.setDescription('一个测试命令')
async execute(interaction) {
const event_manager = new GuildScheduledEventManager('1039213773216546939')
await event_manager.create({
name: 'Test Event',
scheduledStartTime: new Date(1687284000000),
privacyLevel: GuildScheduledEventPrivacyLevel.GuildOnly,
entityType: GuildScheduledEventEntityType.Voice,
description: '这是一个测试预定事件',
channel: '1118952214774419569',
image: null,
reason: '使用创建预定事件进行测试'
});
}
}
(我正在使用Node.js)
有人可以帮助我吗?
英文:
With Discord recently adding GuildScheduledEvent
s to their API, I wanted to make a bot that can create events (on SlashCommand), but I can't get the whole GuildScheduledEvent
and GuildScheduledEventManager
to work.
I keep getting errors like this:
TypeError: Cannot read properties of undefined (reading 'options')
at new CachedManager (C:\dev\Discord Bots\node_modules\discord.js\src\managers\CachedManager.js:21:64)
at new GuildScheduledEventManager (C:\dev\Discord Bots\node_modules\discord.js\src\managers\GuildScheduledEventManager.js:17:5)
at Object.execute (C:\dev\Discord Bots\better bot\bot-data\commands\cancel_film.js:11:31)
at Client.<anonymous> (C:\dev\Discord Bots\better bot\main.js:44:17)
at Client.emit (node:events:513:28)
at InteractionCreateAction.handle (C:\dev\Discord Bots\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
at module.exports [as INTERACTION_CREATE] (C:\dev\Discord Bots\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (C:\dev\Discord Bots\node_modules\discord.js\src\client\websocket\WebSocketManager.js:354:31)
at WebSocketManager.<anonymous> (C:\dev\Discord Bots\node_modules\discord.js\src\client\websocket\WebSocketManager.js:238:12)
at WebSocketManager.emit (C:\dev\Discord Bots\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
I know where it gets the error, but I don't understand why.
This is the code from the SlashCommand:
const { SlashCommandBuilder, GuildScheduledEventManager, GuildScheduledEventPrivacyLevel, GuildScheduledEventEntityType } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('test_command')
.setDescription('a test command')
async execute(interaction) {
const event_manager = new GuildScheduledEventManager('1039213773216546939')
await event_manager.create({
name: 'Test Event',
scheduledStartTime: new Date(1687284000000),
privacyLevel: GuildScheduledEventPrivacyLevel.GuildOnly,
entityType: GuildScheduledEventEntityType.Voice,
description: 'This is a test Scheduled Event',
channel: '1118952214774419569',
image: null,
reason: 'Testing with creating a Scheduled Event'
});
}
}
(I am using Node.js btw)
Is there anybody that can help me?
答案1
得分: 1
我认为这是因为GuildScheduledEventManager
构造函数接受一个服务器(guild)而1039213773216546939
是一个服务器ID(?),而不是服务器对象。
在构造函数内部,使用super
调用了guild.client
。由于您的服务器是一个字符串,它的client
属性是undefined
,然后您将此undefined
传递给CachedManager
。在CachedManager
的构造函数中,它无法读取undefined
的options
属性,因此会抛出“TypeError: Cannot read properties of undefined (reading 'options')”错误。
要解决这个问题,您需要传递一个服务器对象而不是字符串。您可以从interaction.client.guilds
获取它:
module.exports = {
data: new SlashCommandBuilder()
.setName('test_command')
.setDescription('a test command'),
async execute(interaction) {
const guildID = '1039213773216546939';
const guild = interaction.client.guilds.cache.get(guildID);
if (!guild)
return console.log('Guild not found');
const event_manager = new GuildScheduledEventManager(guild);
await event_manager.create({
name: 'Test Event',
scheduledStartTime: new Date(1687284000000),
privacyLevel: GuildScheduledEventPrivacyLevel.GuildOnly,
entityType: GuildScheduledEventEntityType.Voice,
description: 'This is a test Scheduled Event',
channel: '1118952214774419569',
image: null,
reason: 'Testing with creating a Scheduled Event',
});
},
};
英文:
I think it's because the GuildScheduledEventManager
constructor accepts a guild and 1039213773216546939
is a guild ID(?), not a guild object.
Inside the constructor, super
is called with guild.client
. As your guild is a string, its client
property is undefined
and you pass this undefined
to CachedManager
. In the CachedManager
's constructor, it can't read the options
property of undefined
so it throws "TypeError: Cannot read properties of undefined (reading 'options')".
To fix this, you need to pass down a guild instead. You can get it from interaction.client.guilds
:
module.exports = {
data: new SlashCommandBuilder()
.setName('test_command')
.setDescription('a test command'),
async execute(interaction) {
const guildID = '1039213773216546939';
const guild = interaction.client.guilds.cache.find(guildID);
if (!guild)
return console.log('Guild not found');
const event_manager = new GuildScheduledEventManager(guild);
await event_manager.create({
name: 'Test Event',
scheduledStartTime: new Date(1687284000000),
privacyLevel: GuildScheduledEventPrivacyLevel.GuildOnly,
entityType: GuildScheduledEventEntityType.Voice,
description: 'This is a test Scheduled Event',
channel: '1118952214774419569',
image: null,
reason: 'Testing with creating a Scheduled Event',
});
},
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论