简体   繁体   English

Discord.js Bots // 说命令

[英]Discord.js Bots // Say Command

I've coded a "say command" that's supposed to execute a message every time I type in -say.我已经编写了一个“say 命令”,它应该在我每次输入 -say 时执行一条消息。 It's working fine, I just want the prefix to be set to "?"它工作正常,我只想将前缀设置为“?” instead of "-" only for that one command, the other ones are supposed to be set to the main one ("-").而不是仅用于该一个命令的“-”,其他的应该设置为主命令(“-”)。 On top of that I want it to delete the command after typing it in, so all that remains is the message [(eg ?say hello --> (delete "?say hello" --> send message "hello" to text channel)]. I also want to specify the text channel in my command, instead of just setting it to send the messages only to one specific channel [eg -say (message) (text channel)] It would also be pretty cool if it said something like "Done." and deleting that confirmation after ~5 sec.最重要的是,我希望它在输入命令删除命令,所以剩下的就是消息[(eg ?say hello --> (delete "?say hello" --> send message "hello" to text channel)].我还想在我的命令中指定文本通道,而不是仅仅将其设置为仅将消息发送到一个特定通道[eg -say (message) (text channel)]如果它说也很酷类似“完成。”并在约 5 秒后删除该确认。

So here is the code:所以这里是代码:

client.on('message', function(message) {

    if(message.author.bot) return;

    else if(isValidCommand(message, "say")) {

        let sendMessage = message.content.substring(4);

        let sendChannel = client.channels.cache.get('767374258492932106');

        sendChannel.send(sendMessage)
    }
});

In the following I will show you my not working code, trying to set the prefix to "?"在下面,我将向您展示我不工作的代码,尝试将前缀设置为“?” but it didnt execute, only saying "declaration or statement expecting and missing or wrong punctuation..但它没有执行,只说“声明或声明期待和丢失或错误的标点符号..

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

    else if(isValidCommand(message, "say")) {

        let sendMessage = message.content.substring(4);

        let sendChannel = client.channels.cache.get('767374258492932106');

        sendChannel.send(sendMessage)
    }
});

It'd be really grateful if someone helped me with this.如果有人帮助我,我将非常感激。 Thank you!谢谢!

I am not sure what you trying to achieve with我不确定你想要达到什么目的

    if (['?'].every((prefix) => {

but this is what I would do但这就是我要做的

using args, substring and switch to identify the command.使用 args、substring 和 switch 来识别命令。

The usage of the的用法

client.on('message', async message =>{
    if (message.author.bot) return;
    if (message.content.startsWith(prefix)) {
        let args = message.content.substring(prefix.length).split(" ");
        switch (args[0].toLowerCase()){
            case 'say': {
        let sendMessage = message.content.substring(prefix.length +args[0].length+ args[1].length + 2); //2 is accounting for the 2 space between prefix and # and prefix and the main content
                setTimeout(()=>{message.delete()},5000)
                let sendChannel = client.channels.cache.get(args[1]); 
                sendChannel.send(sendMessage)
                break;
            }
        }
    }
    if (message.content.startsWith(otherPrefix))    {
        let args = message.content.substring(otherPrefix.length).split(" ");
        switch (args[0].toLowerCase()){
            case 'say': {
        // Do something else
                break;
            }
        }
    }
});

Edit: The usage of the commend would be like编辑:推荐的用法就像

!say #generalTextChannel abc

where #generalTextChannel is tagging the channel #generalTextChannel 标记频道的地方

or要么

!say 767374258492932106 abc

where 767374258492932106 is the channel Id其中 767374258492932106 是频道 ID

I don't quite understand what are you trying to do with the prefixes.我不太明白你想用前缀做什么。 When trying to detect a prefix in front of a command, you can just use the other line you used without ['?'].every当尝试检测命令前面的前缀时,您可以使用没有['?'].every的另一行

Use just this: if (!message.content.startsWith(prefix) || message.author.bot) return;只使用这个: if (!message.content.startsWith(prefix) || message.author.bot) return; . . Then check for each command individually under this.然后在此下单独检查每个命令。 if (command == 'say')

A very simple way of getting arguments:获取参数的一种非常简单的方法:

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

The say command would look something like this: say 命令看起来像这样:

if(command == 'say'){
   //then you could find the channel
   //https://discord.js.org/#/docs/main/stable/class/ChannelManager?scrollTo=fetch
   client.channels.fetch('222109930545610754')
       .then(channel => {
         channel.send(args.join(" ")) //sending the arguments joined with a space in the fetched channel
            .then(msg => {setTimeout(function(){msg.delete()},5000)}) //delete after 5 seconds, please check if delete is a function (I didn't)
       })
       .catch(console.error);
}

['?'].every got me thinking you could use if(!['?', '-'].some(prefix => content.startsWith(prefix))) return to use more prefixes instead of the if statement. ['?'].every让我觉得你可以使用if(!['?', '-'].some(prefix => content.startsWith(prefix))) return来使用更多的前缀而不是 if 语句。

I don't know what your saying.我不知道你在说什么。 But here is how I would do it.但这是我将如何做到的。

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

client.on('message', message => {

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

if (command === 'say') {
if (!message.content.startsWith(prefix) || message.author.bot) return;

const user = message.author;

if (!args[0]) {
user.send("Provide a word to say in the say command\nExample: !say Hello")
}

const say = args.join(" ");
message.channel.send(say)
message.delete()
}

})

client.login("PUT YOUR TOKEN HERE")

For the other prefix, you could do this:对于其他前缀,您可以这样做:

client.on('message', async message => {

if(message.content === '?say') {
//the code (I don't know what is the code, I'm giving the way for the prefix)
} else {}

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

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

//bla bla bla (the rest of the commands, etc.)
}


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

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