Javascript Discord.js Bots//Say命令

Javascript Discord.js Bots//Say命令,javascript,discord,discord.js,bots,Javascript,Discord,Discord.js,Bots,我编写了一个“say命令”,它应该在我每次输入say时执行一条消息。 它工作得很好,我只希望前缀设置为“?”而不是“-”,仅对于一个命令,其他命令应该设置为主命令(“-”)。除此之外,我希望它在输入命令后删除该命令,因此剩下的就是消息[(例如,say hello-->(删除)?say hello-->将消息“hello”发送到文本频道)].我还想在我的命令中指定文本频道,而不是仅仅将其设置为只将消息发送到一个特定频道[例如-say(message)(text channel)]如果它说“Done

我编写了一个“say命令”,它应该在我每次输入say时执行一条消息。 它工作得很好,我只希望前缀设置为“?”而不是“-”,仅对于一个命令,其他命令应该设置为主命令(“-”)。除此之外,我希望它在输入命令后删除该命令,因此剩下的就是消息
[(例如,say hello-->(删除)?say hello-->将消息“hello”发送到文本频道)].
我还想在我的命令中指定文本频道,而不是仅仅将其设置为只将消息发送到一个特定频道
[例如-say(message)(text channel)]
如果它说“Done”(完成)之类的话,并在5秒后删除确认,那将是非常酷的

下面是代码:

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)
    }
});
在下面,我将向您展示我的无效代码,尝试将前缀设置为“”,但它没有执行,只是说“声明或语句需要,缺少或错误的标点..”

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)
    }
});

如果有人能帮我做这件事,我将不胜感激。谢谢!

我不太明白你想用前缀做什么。 在尝试检测命令前面的前缀时,您可以使用不带
['?']的另一行。每行

仅使用以下命令:
如果(!message.content.startsWith(prefix)| | message.author.bot)返回;
。 然后在此项下分别检查每个命令。
if(command=='say')

const args=message.content.slice(prefix.length.trim().split(+/+/g);
const命令=args.shift().toLowerCase();
say命令将如下所示:

if(命令=='say'){
//然后你就可以找到频道了
//https://discord.js.org/#/docs/main/stable/class/ChannelManager?scrollTo=fetch
client.channels.fetch('2221093054565610754')
。然后(频道=>{
channel.send(args.join(“”)//发送与获取的通道中的空格连接的参数
.then(msg=>{setTimeout(function(){msg.delete()},5000)})//5秒后删除,请检查删除是否是函数(我没有)
})
.catch(控制台错误);
}

['?']。每一个
都让我想到你可以使用
if(!['?','-'])。一些(prefix=>content.startsWith(prefix)))返回
以使用更多前缀,而不是if语句。

我不确定你想用它来实现什么

    if (['?'].every((prefix) => {
但这就是我要做的

使用args、substring和switch标识命令

使用

client.on('message',异步消息=>{
if(message.author.bot)返回;
if(message.content.startsWith(前缀)){
设args=message.content.substring(prefix.length).split(“”);
开关(参数[0].toLowerCase()){
案例“说”:{
让sendMessage=message.content.substring(prefix.length+args[0].length+args[1].length+2);//2占前缀和#之间的2个空格,前缀和主内容
setTimeout(()=>{message.delete()},5000)
让sendChannel=client.channels.cache.get(args[1]);
sendChannel.send(sendMessage)
打破
}
}
}
if(message.content.startsWith(otherPrefix)){
让args=message.content.substring(otherPrefix.length).split(“”);
开关(参数[0].toLowerCase()){
案例“说”:{
//做点别的
打破
}
}
}
});
编辑:command的用法如下

!say #generalTextChannel abc
其中#generalTextChannel正在标记该频道


其中76737425849293206是频道Id,我不知道你在说什么,但我会这样做

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")

您似乎在要求他人为您编写一些代码。Stack Overflow是一个问答网站,而不是代码编写服务。请学习如何编写有效的问题。
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")