You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js中调用gameid命令时placeId参数未定义问题求助

问题分析:gameid命令中placeId未定义的原因

这问题其实出在数组索引的变化上,咱们拆解一下逻辑就清楚了:

你用args.shift()把命令名(也就是gameid)从数组里移除了,这时候args的结构已经完全变了。举个实际例子:
假设用户输入的是!gameid 12345,那么:

  • 初始args数组是['gameid', '12345']
  • 执行args.shift()后,command拿到了gameid,剩下的args就只剩['12345'],长度为1

这时候你去取args[1],自然会得到undefined——因为这个索引对应的元素根本不存在,你应该取的是args[0]。

修正后的代码片段

let args = message.content.slice(config.prefix.length).split(' '); 
let command = args.shift().toLowerCase(); 
let tellJoke = randomNumber(4); 
if (debounce == true){ 
  return 
} 
debounce = true 
switch (command) { 
  case 'gameid': 
    console.log(args.length) 
    if(args.length == 1){ 
      // 这里把args[1]改为args[0]
      let placeId = args[0] 
      console.log(placeId) 
      api = await fetch("https://api.roblox.com/universes/get-universe-containing-place?placeid=" + placeId) 
      let gameId = await api.json() 
      try{ 
        if(api){ 
          reply = await message.reply('Game Id: ' + gameId.UniverseId) 
          debounce = false 
          break; 
        } 
      }catch(error){ 
        reply = await message.reply('Something went wrong. ERROR: ' + error); 
        debounce = false 
        break; 
      } 
    } 
    reply = await message.reply('Something went wrong. Did you write the command correctly?') 
    debounce = false 
    return

额外小建议

可以给args加个过滤处理,避免用户输入多个空格导致的空元素问题:

let args = message.content.slice(config.prefix.length).split(' ').filter(Boolean);

这样能让命令的输入容错性更强。

内容的提问来源于stack exchange,提问作者KLB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 03:02:42