为啥 startWithPrefix 在 discord.js v13 中不起作用?

Posted

技术标签:

【中文标题】为啥 startWithPrefix 在 discord.js v13 中不起作用?【英文标题】:Why startWithPrefix not working in discord.js v13?为什么 startWithPrefix 在 discord.js v13 中不起作用? 【发布时间】:2022-01-02 19:01:26 【问题描述】:

我想将我的真心话大冒险机器人 discord.js v12 更新到 v13。现在我在使用 ping pong 时遇到了一些问题,它工作正常,但是当我想用我的前缀做一些事情时,它给了我这个错误。我从未使用过 discord.js v13,但我的一位朋友说 discord.js v12 将消失,discord 将停用这些机器人

> node .

Truth Or Dare 2.0#2237 Has logged in
(node:107) DeprecationWarning: The message event is deprecated. Use messageCreate instead
(Use `node --trace-deprecation ...` to show where the warning was created)
/home/runner/TOD/node_modules/discord.js/src/rest/RequestHandler.js:349
      throw new DiscordAPIError(data, res.status, request);
            ^

DiscordAPIError: Cannot send an empty message
    at RequestHandler.execute (/home/runner/TOD/node_modules/discord.js/src/rest/RequestHandler.js:349:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async RequestHandler.push (/home/runner/TOD/node_modules/discord.js/src/rest/RequestHandler.js:50:14)
    at async TextChannel.send (/home/runner/TOD/node_modules/discord.js/src/structures/interfaces/TextBasedChannel.js:172:15) 
  method: 'post',
  path: '/channels/874498102139166730/messages',
  code: 50006,
  httpStatus: 400,
  requestData: 
    json: 
      content: undefined,
      tts: false,
      nonce: undefined,
      embeds: undefined,
      components: undefined,
      username: undefined,
      avatar_url: undefined,
      allowed_mentions: undefined,
      flags: undefined,
      message_reference: undefined,
      attachments: undefined,
      sticker_ids: undefined
    ,
    files: []
  

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! TOD@1.0.0 start: `node .`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the TOD@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/runner/.npm/_logs/2021-11-24T08_19_36_335Z-debug.log
exit status 1

现在我不知道错误是什么。对我来说它看起来很奇怪,我不知道为什么,但 v13 就像 ewww。 有没有人可以帮帮我,拜托

这是我在 discord v13 中使用的代码。我只是改变了客户线其余的都是一样的

const Discord = require('discord.js');
let client = new Discord.Client(intents: [
  "GUILDS",
  "GUILD_MESSAGES"
]);

// const disbut = require('discord-buttons');
const prefix = "^";


// ======== Ready Log ========
client.on ("ready", () => 
 
    console.log(`$client.user.tag Has logged in`)
   async function ll() 
        let totalMembers = 0;

        for (const guild of client.guilds.cache) 
            totalMembers += (await guild[1].members.fetch()).size;
        

        var status = '+helme | Truth Or Dare'

        const types = ['PLAYING', 'WATCHING', "STREAMING"]     
        let randomType = types[Math.floor(Math.random() * types.length)]
        if (randomType === 'PLAYING') status = `+helpme | @$client.user.username` 

        if (randomType === 'STREAMING') 
            status = `$totalMembers members`
        
        if (randomType === 'WATCHING') 
            status = `$client.guilds.cache.size servers`
        

    client.user.setPresence(activity: type: randomType, url: "https://www.twitch.tv/", name: status, status:  'STREAMING');

    //     client.user.setPresence(activity: type: types[Math.floor(Math.random() * types.length)], url: "https://www.twitch.tv/", name: `$status[Math.floor(Math.random() * status.length)]`, status:  ss[Math.floor(Math.random() * ss.length)]);



setInterval(() => 
    ll()
, 5000)
  ); 
// ======== Code ========

// Array of possible truth replies
const t = [
    "If you could be invisible, what is the first thing you would do?", 
    "What is a secret you kept from your parents?", 
    "What is the most embarrassing music you listen to?", 
    "What is one thing you wish you could change about yourself?",
    "Who is your secret crush?"
];

// Array of possible dare replies
const d = [
    "Do a free-style rap for the next minute.",
    "Let another person post a status on your behalf.",
    "Hand over your phone to another player who can send a single text saying anything they want to anyone they want.",
    "Let the other players go through your phone for one minute.",
    "Smell another player's armpit",
    "Smell another player's bare foot.",
    "Eat a bite of a banana peel",
    "Do an impression of another player until someone can figure out who it is.",
    "Take a selfie right now and send it here",
    "Say I love to your Crush"
];

const tb = [
  "যদি আপনি অদৃশ্য হতে পারেন, আপনি প্রথম জিনিস কি করবেন?",
  "কোন সিক্রেট কথা আপনি আপনার বাবা-মায়ের কাছ থেকে গোপন রেখেছিলেন?",
  "আপনার শোনা সবচেয়ে বিব্রতকর সঙ্গীত কোনটি?"
]
const db = [
  "এক মিনিটের জন্য একটি ফ্রি-স্টাইল রেপ করুন।",
  "সোশ্যাল মিডিয়ায় পোস্ট দিন ``আমি একজন পাগল ব্যাক্তি``",
  "এখানের কাওকে এক মিনিটের জন্য ফোন করুন।",
  "তোমার ক্রাশকে মেসেজ দাও ``আমি তোমাকে ভালোবাসি`` এবং ‍স্ক্রিনশট  এখানে দাও",
]

client.on('message', message => 
  if(message.content === 'ping')
    message.channel.send('pong')
  

  if (message.author.bot || !message.guild || !message.content.startsWith(prefix)) return;

  const command = message.content.substring(prefix.length);

  // Match the command
  if (command === "t")  // Truth
    const truth = t[Math.floor(Math.random() * t.length)];
    message.channel.send(new Discord.MessageEmbed()
      .setColor('#FAA81A')
      .setURL('https://discord.com/api/oauth2/authorize?client_id=874488895037911041&permissions=259846043712&scope=bot')
      .setTitle("Truth")
      .setDescription(truth))
    message.react("????");
  
  else if (command === "d")  // Dare
    const dare = d[Math.floor(Math.random() * d.length)];
    message.channel.send(new Discord.MessageEmbed()
      .setColor('#7B00F7')
      .setURL('https://discord.com/api/oauth2/authorize?client_id=874488895037911041&permissions=259846043712&scope=bot')
      .setTitle("Dare")
      .setDescription(dare))
    message.react("????");
  
  else if (command === "tb")  // Truth
    const truth = tb[Math.floor(Math.random() * tb.length)];
    message.channel.send(new Discord.MessageEmbed()
      .setColor('#FAA81A')
      .setURL('https://discord.com/api/oauth2/authorize?client_id=874488895037911041&permissions=259846043712&scope=bot')
      .setTitle("Truth Bangla")
      .setDescription(truth))
    message.react("????");
  
    else if (command === "db")  // Dare
    const dare = db[Math.floor(Math.random() * db.length)];
    message.channel.send(new Discord.MessageEmbed()
      .setColor('#7B00F7')
      .setURL('https://discord.com/api/oauth2/authorize?client_id=874488895037911041&permissions=259846043712&scope=bot')
      .setTitle("Dare Bangla")
      .setDescription(dare))
    message.react("????");
  
  else if (command === "inviteme") 
    const invite = new Discord.MessageEmbed()
      .setColor('3a86ff')
      .setTitle("Invite Me")
      .setURL('https://discord.com/api/oauth2/authorize?client_id=874488895037911041&permissions=259846043712&scope=bot')
      .setDescription("Click on **[Invite Me]('https://discord.com/api/oauth2/authorize?client_id=874488895037911041&permissions=259846043712&scope=bot')** to invite this bot on your server ")

    message.channel.send(invite)
    message.react("????");
  
  else if (command === "vote")
    
      const vote = new Discord.MessageEmbed()
      .setColor('3a86ff')
      .setTitle("Vote Me")
      .setURL('https://discord.com/api/oauth2/authorize?client_id=874488895037911041&permissions=259846043712&scope=bot')
      
      .setDescription(`[discordbotlist.com](https://discordbotlist.com/bots/truth-or-dare-7260/upvote) Click on **[Vote]('https://discordbotlist.com/bots/truth-or-dare-7260/upvote')** or click the button down bellow to Vote Me`)
    let button = new disbut.MessageButton()
  .setStyle('url')
  .setURL('https://discordbotlist.com/bots/truth-or-dare-7260/upvote') 
  .setLabel('Vote Me') 
  .setDisabled(); 
    message.channel.send(vote)
    message.react("????");
  
  // else if (message, "post") 
  //   let announcement = message.content.substring(6);
  //   let announcementsChannel = client.channels.cache.get('874498102139166731');
  //   if (announcementsChannel)
  //     announcementsChannel.send(announcement)
  // 

  else if (command === "helpme")  // Help

    const help = new Discord.MessageEmbed()
      .setColor('#111133')
      .setTitle("Truth Or Dare")
      .setURL('https://discord.com/api/oauth2/authorize?client_id=874488895037911041&permissions=259846043712&scope=bot')
      .addFields(
         name: 'For Help', value: '```+helpme```' ,
         name: 'For Your Truth', value: '```+t```', inline: true ,
         name: 'For Your Dare', value: '```+d```', inline: true,
         name: 'For Invite this bot on your server', value: '```+inviteme```' ,
         name: 'For Truth Questions Bangla', value:'```+tb```', inline: true,
         name: 'For Dare Questions Bangla', value:'```+db```', inline: true,
         name: 'Created By', value: '<@723821826291138611> [**Leader at CODE HUNTER**]' ,
      )
      .setDescription(
        `Truth Or Dare Bot Version: v$require("./package.json").version
  [Website](https://web-truthordare.web.app/) | [Support Server](https://discord.gg/djhNPX2QUp) | By [Code Hunter](https://github.com/Code-Hunter-OfficialBD/)`
      )
      .setTimestamp()
      .setFooter(`$message.author.username `, message.author.displayAvatarURL());

    message.channel.send(help)
    message.react("✅");

  
   else if (command === "post") 
    const ch = message.mentions.channels.first();
    ch.send(message.content.substring(prefix.length + command.length))
  



);

const token = process.env.TOKEN;

client.login(token);

【问题讨论】:

这能回答你的问题吗? Discord.js v12 code breaks when upgrading to v13 根据其接受的答案,这个问题不是前面评论中链接的问题的重复。 【参考方案1】:

问题不在于您的前缀,而是由于您发送的嵌入不正确。

您当前正在发送嵌入,例如:

    message.channel.send(new Discord.MessageEmbed()
      .setColor('#7B00F7')
      .setURL('https://discord.com/api/oauth2/authorize?client_id=874488895037911041&permissions=259846043712&scope=bot')
      .setTitle("Dare")
      .setDescription(dare))

发送嵌入的正确方法如下:

    message.channel.send(
         embeds: [new Discord.MessageEmbed()
          .setColor('#7B00F7')
          .setURL('https://discord.com/api/oauth2/authorize? 
    client_id=874488895037911041&permissions=259846043712&scope=bot')
          .setTitle("Dare")
          .setDescription(dare)]
        

【讨论】:

现在,这里有什么问题? cdn.discordapp.com/attachments/897555341171122257/… 有一个额外的括号,现在已删除

以上是关于为啥 startWithPrefix 在 discord.js v13 中不起作用?的主要内容,如果未能解决你的问题,请参考以下文章

为啥从 STM32F407G-Disc1 MCU 上的 LIS3DSH 加速度计读取时我只得到 0xFF?

如何在日期时间内聚合 percentile_disc() 函数

STM32F429-DISC1 LCD打印双像素

DISC测试工具,帮助你成为行为判断专家

PostgreSQL 中的 PERCENTILE_DISC() 作为窗口函数

利用canvas实现disc测评结果视图展示