简体   繁体   English

为什么我的 discord 机器人不发送消息?

[英]Why is my discord bot not sending messages?

My discord bot is not sending any messages, when I type +hi , nothing responds.我的 discord 机器人没有发送任何消息,当我输入+hi时,没有任何响应。 Although, it is sending the message when it's turned on.虽然,它在打开时发送消息。 When I type +hi , nothing happens.当我输入+hi时,没有任何反应。

当机器人打开时,它会发送消息它什么也没做

Here is my code.这是我的代码。

// - Discord Requires
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = '+';
// - Imports
const date = require('date-and-time');
const fs = require('fs');
const express = require("express")();
// - Embed
const { MessageEmbed } = require('discord.js');
// - Logo
const logo = './images/logo.png'
// - On Start
client.once('ready', () =>{
  //- Status and Conlole log Message
  client.user.setPresence({activity: {name:"Counting-Days", type:'WATCHING'}, status:'online'})
  .then(console.log)
  .catch(console.error);
  setTimeout(function(){
    console.log("🍉I'm ready, from now on I'm gonna count Christmas Days!:)🍉");
  }, 2000);
  // - Current Date and Time
  const now_date = new Date();
  date.format(now_date, 'YYYY/MM/DD HH:mm:ss');
  date.format(now_date, 'ddd, MMM DD YYYY');
  date.format(now_date, 'hh:mm A [GMT]Z');
  date.format(now_date, 'hh:mm A [GMT]Z', true);
  // - Makes the pattern for time
  const time_pattern = date.compile('hh:mm A');
  const time = date.format(now_date, time_pattern);
  const date_pattern = date.compile('ddd, MMM DD YYYY')
  const date_a = date.format(now_date, date_pattern);
  // - Channel Message => Ready
  client.channels.cache.get('935621866926768159').send(`**🎄Christmas Countdown Bot🎄** is awake |Date: **${date_a}**, Time: **${time}**| and waiting to reply to |${prefix}| commands!✅`)
});

// - Calculates the Days until Christmas (25/12/2022)
const newYears = "25 Dec 2022";

const newYearsDate = new Date(newYears);
const currentDate = new Date();

const totalSeconds = (newYearsDate - currentDate) / 1000;
const days = Math.floor(totalSeconds / 3600 / 24);
const hours = Math.floor(totalSeconds / 3600) % 24;
const mins = Math.floor(totalSeconds / 60) % 60;
const seconds = Math.floor(totalSeconds) % 60;

client.on("message", function (message){
  if (message.content === `${prefix}hi`){
    message.channel.send(`Hey, ${days}`);
  }
})
// -Token
client.login('TOKEN')

I've tried restarting the bot several times but it didn't work.我试过多次重启机器人,但没有用。 I also tried the code on another bot and it worked.我还在另一个机器人上尝试了代码并且它有效。

The problem most likely lies on your message function. Discord.js has replaced the old client.on("message... and replaced it with client.on("messageCreate... .问题很可能出在您的消息 function 上。Discord.js 已替换旧的client.on("message...并将其替换为client.on("messageCreate...

You've done:你做了:

client.on("message", function (message){
  if (message.content === `${prefix}hi`){
    message.channel.send(`Hey, ${days}`);
  }
})

The correct way to do this is as follows:正确的做法如下:

client.on("messageCreate", function (message) => {
  if(message.content === `${prefix}hi`){
    return message.channel.send(`Hey, ${days}`);
  };
})

It is likely that you've got two bots with different versions of discord.js, the older discord.js@v13 works with the .on("message) and gives a deprecation error in the console while the newer discord.js@v14 completely ignores the .on("message") as it is no longer an event.您可能有两个具有不同版本 discord.js 的机器人,较旧的.on("message)并在控制台中给出弃用错误,而较新的 discord.js@v14 则完全忽略.on("message")因为它不再是一个事件。

Simply delete the incorrect event listener and replace it with the correct one.只需删除不正确的事件侦听器并将其替换为正确的事件侦听器即可。

It would be better if you had given a discord.js version to help the community understand your problem more.如果能给个 discord.js 版本帮助社区更了解你的问题就更好了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM