在Discord中创建定时事件 | discord.js

huangapple go评论92阅读模式
英文:

Creating a Scheduled Event in Discord | discord.js

问题

由于Discord最近在其API中添加了GuildScheduledEvent,我想制作一个可以创建事件的机器人(在SlashCommand上),但我无法使整个GuildScheduledEventGuildScheduledEventManager正常工作。

我一直收到这样的错误:

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 GuildScheduledEvents 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 &#39;options&#39;)
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.&lt;anonymous&gt; (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.&lt;anonymous&gt; (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(&#39;discord.js&#39;);

module.exports = {
    data: new SlashCommandBuilder()
        .setName(&#39;test_command&#39;)
        .setDescription(&#39;a test command&#39;)
    async execute(interaction) {
        const event_manager = new GuildScheduledEventManager(&#39;1039213773216546939&#39;)

        await event_manager.create({
            name: &#39;Test Event&#39;,
            scheduledStartTime: new Date(1687284000000),
            privacyLevel: GuildScheduledEventPrivacyLevel.GuildOnly,
            entityType: GuildScheduledEventEntityType.Voice,
            description: &#39;This is a test Scheduled Event&#39;,
            channel: &#39;1118952214774419569&#39;,
            image: null,
            reason: &#39;Testing with creating a Scheduled Event&#39;
        });
    }
}

(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的构造函数中,它无法读取undefinedoptions属性,因此会抛出“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(&#39;test_command&#39;)
    .setDescription(&#39;a test command&#39;),

  async execute(interaction) {
    const guildID = &#39;1039213773216546939&#39;;
    const guild = interaction.client.guilds.cache.find(guildID);
    
    if (!guild)
      return console.log(&#39;Guild not found&#39;);

    const event_manager = new GuildScheduledEventManager(guild);

    await event_manager.create({
      name: &#39;Test Event&#39;,
      scheduledStartTime: new Date(1687284000000),
      privacyLevel: GuildScheduledEventPrivacyLevel.GuildOnly,
      entityType: GuildScheduledEventEntityType.Voice,
      description: &#39;This is a test Scheduled Event&#39;,
      channel: &#39;1118952214774419569&#39;,
      image: null,
      reason: &#39;Testing with creating a Scheduled Event&#39;,
    });
  },
};

huangapple
  • 本文由 发表于 2023年6月19日 03:15:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定