[英]Discord.js v13 Announce an Annoucement to a certain Channel
我想做一个命令,将用户(管理员)给定的特定消息发送到特定频道。
我试图向使用他们输入的参数调用命令的用户发送回复。
其次,我这样做了,它将每个 arg 加入到一个数组中,然后回复它。
我怎样才能像我的信息一样放置空格和点?
例如,如果我输入!announce hi hi hi
,则 output 将为hihihi
如果我在我的 args 之间设置空格, !announce Hello World!
,它会回复Hello World !
而不是Hello World!
结果代码:
module.exports = {
name: 'announce',
description: 'Just a test command',
aliases: [],
usage: '[prefix] + announce + channel',
guildOnly: false,
args: false,
permissions: {
bot: [],
user: [],
},
execute: (message, args, client) => {
var out = [];
for (let i = 0; i < args.length; i++) {
out += args[i];
}
console.log(out);
message.reply(out);
},
};
PS:它既没有检测到管理员用户也没有检测到要发送到的频道。
我发现获取文本的另一种方法是通过代码块,但是如何将其数据作为 arg 读取?
这里不需要使用 for 循环。 您可以在args
上使用slice()
来获取消息的内容。
由于您的命令用法如下所示: [prefix] + announce + channel
,我决定不使用message.mentions.channels?.first();
因为成员可以在他的消息中的任何地方提到一个频道。
我还决定在此示例中使用message.content
,因为我不太确定您的args
是否不区分大小写。
您可以做的是获取message.content
并使用.split(" ")
创建一个数组,然后使用slice()
获取您需要的信息。
const messageContent = message.content.split(" ") // array
// We remove the first element (your prefix) from the array, and the last
// element (the channel) to get the announcement message
const announcement = message.content.split(" ").slice(1).slice(0, -1).join(" ")
// Get the channel by accessing the last element in the array
const channel = messageContent[messageContent.length - 1]
// check if the provided channel is a mention or the ID of the channel
if(!channel.match(/<#[0-9]+>/) && isNaN(channel)) return message.reply({content: 'provide a valid channel'})
// search for the channel inside the guild. We replace "<#" and ">" to get the ID
// in case that the channel was mentioned
const foundChannel = await message.guild.channels.cache.get(channel.replace('<#','').replace('>',''));
// if the guild has no channel
if(!foundChannel) return message.reply({content: 'provide a valid channel'})
// send the announcement
foundChannel.send({content: announcement})
如果我能正确理解,您需要一个命令,例如!announce #announcements Some content to send in another channel
that sent Some content to Some content to send in another channel
in the channel ( #announcements
)。
您可以使用message.mentions.channels
检查是否有提及的频道。 它返回一组通道,因此您可以获取.first()
通道。 如果没有,您可以发送错误消息。
另一个错误是您尝试加入 args 的 rest 的方式。 首先,您需要删除第一个参数,即频道名称。 您可以为此使用.slice(1)
。 删除第一项后,您可以将项目的 rest join()
一个空格。
您提到如果您通过空格加入 args,您将收到Hello World !
而不是Hello World!
. 您需要确保仅按空格分隔内容。 像这样的东西:
const args = message.content.slice(prefix.length).split(/ +/);
这是您的代码:
module.exports = {
name: 'announce',
description: 'Just a test command',
aliases: [],
usage: '[prefix] + announce + channel',
guildOnly: false,
args: false,
permissions: {
bot: [],
user: [],
},
execute: (message, args, client) => {
// check if there is a channel mentioned
let channel = message.mentions.channels?.first();
if (!channel)
message.reply('No channel mentioned');
let content = args
// remove the first item in args as that's the channel
.slice(1)
// join the items by a single space
.join(' ');
// send the content to the mentioned channel
channel.send(content);
message.reply(`Your message is sent in ${channel}`);
},
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.