如何使Discord机器人在某个频道中发送消息

时间:2020-07-23 02:27:40

标签: discord

我想知道如何使Discord僵尸程序在某个频道中发送消息,我可能搞砸了其他一些事情,因此请让我知道是否有问题。这是代码

const Discord = require("discord.js");
const client = new Discord.Client();

client 
var SendMessage = true;
const prefix = "!";


client.once("ready",  () => 
{
console.log("Bump_It is online!");
});


client.on("message", message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if(command === "start"){
        SendMessage = true;
    }
    if(command === "Stop"){
        SendMessage = false;
    }
});

if( SendMessage == true){
 
 client.channels.cache.get("test").send("hi");
SendMessage = false;
    setTimeout(() => { SendMessage = true }, 10000);
}

client.login("NzM1NjUwMzMyODA1MzAwMzc0.Xxjihw.CIj0lmUt0z2Y127TAx4-8g8wqe0");

这是错误消息

client.channels.cache.get("test").send("hi");
                                  ^

TypeError: Cannot read property 'send' of undefined
    
at Object.<anonymous> (C:\Users\myusername\Desktop\DiscordBot\main.js:37:35)

[90m    at Module._compile (internal/modules/cjs/loader.js:1137:30)[39m
[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)[39m
[90m    at Module.load (internal/modules/cjs/loader.js:985:32)[39m
[90m    at Function.Module._load (internal/modules/cjs/loader.js:878:14)[39m
[90m    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)[39m
[90m    at internal/main/run_main_module.js:17:47[39m

0 个答案:

没有答案