cloudmaker97 / FS25-Discord-Bot

A simple discord bot for the LS25 / FS25 / Farming Simulator 25 (with docker support) for server status updates in discord embeds. 🚜
https://dennis-heinri.ch/
MIT License
3 stars 1 forks source link

Bot wont start "Cannot read properties of undefined (reading 'Mod')" #10

Closed Scallywer closed 4 days ago

Scallywer commented 4 days ago
2024-11-27 23:12:01 info: Starting | App: ls25-discord-bot | Version: 0.1.3
2024-11-27 23:12:01 info: ----------------------------------------------------
2024-11-27 23:12:01 info: Discord configuration is valid.
2024-11-27 23:12:01 info: Application configuration is valid.
2024-11-27 23:12:01 info: Translation configuration is valid.
2024-11-27 23:12:02 info: Discord client ready. Logged in as FS25bot!
2024-11-27 23:12:02 info: Deleting all messages in discord text channel 1311469288284094516
2024-11-27 23:12:02 info: Fetching server status from feed url
2024-11-27 23:12:02 info: Login successful to discord with token
2024-11-27 23:12:03 info: Server status feed successful received
/app/build/Services/ServerStatusFeed.js:122
        let modList = (_a = this.getServerStats()) === null || _a === void 0 ? void 0 : _a.Server.Mods.Mod;
                                                                                                       ^

TypeError: Cannot read properties of undefined (reading 'Mod')
    at ServerStatusFeed.getServerMods (/app/build/Services/ServerStatusFeed.js:122:104)
    at DiscordEmbed.<anonymous> (/app/build/Services/DiscordEmbed.js:135:46)
    at Generator.next (<anonymous>)
    at /app/build/Services/DiscordEmbed.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (/app/build/Services/DiscordEmbed.js:4:12)
    at DiscordEmbed.generateEmbedFromStatusFeed (/app/build/Services/DiscordEmbed.js:107:16)
    at DiscordEmbed.<anonymous> (/app/build/Services/DiscordEmbed.js:61:26)
    at Generator.next (<anonymous>)
    at /app/build/Services/DiscordEmbed.js:8:71

Node.js v23.3.0

Config file (obfuscated a bit):

{
  "application": {
    "serverPassword": "",
    "serverStatsUrl": "http://si-xxxx.xx.xxxxxxxxx.xxx:5006/feed/dedicated-server-stats.xml?code=6pv8ckce1nbk38s4mm3zv2cywcwp",
    "serverMapUrl": "http://si-xxxx.xx.xxxxxxxxx.xxx:5006/feed/dedicated-server-stats-map.jpg?code=6pv8ckce1nbk38s4mm3zv2cywcwp&quality=60&size=512",
    "updateIntervalSeconds": 30
  },
  "discord": {
    "channelId": "1311469288284094xxx",
    "botToken": "MTI4MDYxMTE1NTYxODAzNzc2MQ.GX-V9Q._3vRBTkkPxEiemKY0hL0ncaQV6c1OP_IUJzxxx"
  },
Scallywer commented 4 days ago
public getServerMods(): IMod[] {
    const stats = this.getServerStats();

    // Validate the existence of the required structure
    if (!stats || !stats.Server?.Mods?.Mod) {
        return []; // Return an empty array if mods are unavailable
    }

    // Normalize modList and assert its type
    let modList: { [k: string]: unknown; name?: string; author?: string; version?: string; hash?: string; _text?: string; }[] =
        Array.isArray(stats.Server.Mods.Mod)
            ? stats.Server.Mods.Mod
            : [stats.Server.Mods.Mod];

    // Map modList to IMod
    return modList.map((mod) => ({
        name: mod.name || 'Unknown Mod',
        author: mod.author || 'Unknown Author',
        version: mod.version || 'Unknown Version',
        hash: mod.hash || '',
        _text: mod._text || '',
    }) as IMod);
}
cloudmaker97 commented 3 days ago

Thank you @Scallywer for reporting this issue. I've released a hotfix for this issue. Please update your project via git pull and restart the project via one of these methods: