简体   繁体   English

我正在使用 discord.js 编写一个 discord 机器人,我正在处理一个建议命令

[英]I'm coding a discord bot using discord.js and I'm working on a suggestion command

I'm currently trying to get the bot to send a filler message to the channel I eventually want the suggestions to go to and have tried multiple methods to do so, but keep getting errors.我目前正在尝试让机器人向频道发送填充消息,我最终希望向 go 发送建议,并尝试了多种方法,但不断出现错误。 I'm sure the solution is very simple, and any help is much appreciated.我确信解决方案非常简单,非常感谢任何帮助。

suggest.js建议.js

module.exports = {
    name: 'suggest',
    aliases: ['suggestion'],
    description: 'Sends a suggestion to the <#700591796119535657> channel.',
    usage: '<your suggestion>',
    cooldown: 1,
    execute(message, args) {
        const Discord = require('discord.js');
        const client = new Discord.Client();
        const channel = client.channels.cache.get('701087240729657457');
        channel.send('test');
    },
};

error message in console upon running.suggest运行时控制台中的错误消息。建议

TypeError: Cannot read property 'send' of undefined
    at Object.execute (/home/shares/public/RetroCraft/commands/suggest.js:11:11)
    at Client.<anonymous> (/home/shares/public/RetroCraft/index.js:69:11)
    at Client.emit (events.js:310:20)
    at MessageCreateAction.handle (/home/shares/public/RetroCraft/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (/home/shares/public/RetroCraft/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/home/shares/public/RetroCraft/node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (/home/shares/public/RetroCraft/node_modules/discord.js/src/client/websocket/WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (/home/shares/public/RetroCraft/node_modules/discord.js/src/client/websocket/WebSocketShard.js:293:10)
    at WebSocket.onMessage (/home/shares/public/RetroCraft/node_modules/ws/lib/event-target.js:120:16)
    at WebSocket.emit (events.js:310:20)

index.js索引.js

const fs = require('fs');
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');

const cooldowns = new Discord.Collection();
const client = new Discord.Client();
client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);

    // set a new item in the Collection
    // with the key as the command name and the value as the exported module
    client.commands.set(command.name, command);
}

client.once('ready', () => {
    console.log('Ready!');
    client.user.setActivity('RetroCraft', { type: 'WATCHING' });
});

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

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

    const command = client.commands.get(commandName)
        || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));

    if (!command) return;

    if (command.guildOnly && message.channel.type !== 'text') {
        return message.reply('I can\'t execute that command inside DMs!');
    }

    if (command.args && !args.length) {
        let reply = `You didn't provide any arguments, ${message.author}!`;

        if (command.usage) {
            reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
        }

        return message.channel.send(reply);
    }
    if (!cooldowns.has(command.name)) {
        cooldowns.set(command.name, new Discord.Collection());
    }

    const now = Date.now();
    const timestamps = cooldowns.get(command.name);
    const cooldownAmount = (command.cooldown || 3) * 1000;

    if (timestamps.has(message.author.id)) {
        const expirationTime = timestamps.get(message.author.id) + cooldownAmount;

        if (now < expirationTime) {
            const timeLeft = (expirationTime - now) / 1000;
            return message.reply(`please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${command.name}\` command.`);
        }
    }

    timestamps.set(message.author.id, now);
    setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);

    try {
        command.execute(message, args);
    }
    catch (error) {
        console.error(error);
        message.reply('there was an error trying to execute that command!');
    }
});

client.login(token);

This is a really easy fix.这是一个非常简单的修复。 Just change the channel.send to message.channel.send and that should work.只需将channel.send更改为message.channel.send即可。 Tell me if you get any errors and I'll try to help:)如果您遇到任何错误,请告诉我,我会尽力提供帮助:)

暂无
暂无

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

相关问题 我正在使用 VSC 中的 discord.js 编写 discord 机器人,所有命令都在响应,除了一个,这是我正在尝试创建的票证命令 - I'm coding a discord bot using discord.js in VSC and all commands are responding except one, which is a ticket command I'm trying to create 我想让我的 discord.js 机器人不可见 - I'm looking to make my discord.js bot invisible 我在 discord.js 中制作头像机器人时出错 - I'm getting an error in making avatar bot in discord.js 我的清除命令 (Discord.js) 有问题 - I'm having an issue with my Purge command (Discord.js) 有什么办法可以修复我正在创建的 discord 机器人 discord.js - Is there any way i can fix my discord bot, which i`m creating discord.js 我正在编写一个 discord 机器人,但是当我使用 ping 命令时,它会显示 0 毫秒。 我应该怎么办? - I'm coding a discord bot, but when I'm using the ping command its shows me 0 ms. what should i do? 我在使用 discord.js 时遇到问题 - I'm having trouble with discord.js 我在使用 discord.js 中的 API 时遇到问题 - I'm having issues using APIs in discord.js 我正在使用 Discord.js 制作机器人。 这个机器人将用户保存到一个数组中,以便稍后对他们进行 dm。 我收到错误 - i'm using Discord.js to make a bot. This bot saves users into an array to later dm them. i'm getting errors 我正在制定计划 Discord.js 机器人,但它没有发送消息 - I'm making a schedule Discord.js bot, but it's not sending a message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM