[英]How to make a discord.js bot say custom messages in a specific channel & tag a user in a message
So, I wanted to create my own discord server for my friends and me, and I wanted it to have some bot features... So I got into coding.. long story short I messed it up and after hours of trying to find the solution I gave up.所以,我想为我和我的朋友创建我自己的 Discord 服务器,我希望它有一些机器人功能......所以我开始编码......长话短说,我把它搞砸了,经过几个小时的努力寻找我放弃的解决方案。
The first one is a word filter, works perfectly fine, I just cannot tag anyone.. It's supposed to delete the message that's said and give out a message, just like "@example, don't say that!"第一个是单词过滤器,工作得很好,我只是不能标记任何人..它应该删除所说的消息并发出消息,就像“@example,不要那样说!”
client.on('message', async(msg) => {
if(msg.author.bot) return;
if(!msg.guild) return;
var array = ['example1, 'example2'];
if(!msg.member.hasPermission('MANAGE_MESSAGES')){
if(array.some(w => ` ${msg.content.toLowerCase()} `.includes(` ${w}`))){
msg.delete();
msg.channel.send(`${client.user.tag}`)
return;
}
}
});
My second problem: I tried creating a command that every time you type in (prefix)say + "text"
on Discord, the bot will say that message.我的第二个问题:我尝试创建一个命令,每次您在 Discord 上输入
(prefix)say + "text"
时,机器人都会说出该消息。 the best would be if it's possible that you type in all the commands in ONE text-channel and have the ability to choose to what channel I want the bot to type that message too.最好的办法是,如果您可以在一个文本通道中输入所有命令,并且能够选择我希望机器人也输入该消息的通道。 the simple variant would be fine aswell for me.
简单的变体对我来说也很好。 here is my code:
这是我的代码:
const isValidCommand = (message, cmdName) => message.content.toLowerCase().startsWith(prefix + cmdName);
require('dotenv').config();
client.on('message', function(message) {
if(message.author.bot) return;
else if(isValidCommand(message, "say")) {
let marketing = message.content.substring(5);
let marketingchannel = client.channels.cache.get('766732225185054740');
let logsChannel = client.channels.cache.find(channel => channel.name.toLowerCase() === 'logs');
if(logsChannel)
logsChannel.send(marketing);
}
});
I really hope that I will get it to work..我真的希望我能让它工作..
Thanks in advance!提前致谢!
User.tag
doesn't actually mention the user. User.tag
实际上并未提及用户。 A user's tag in a combination of their username
and discriminator
.用户的标签结合了他们的
username
和discriminator
。
'Lioness100'; // my username
'4566'; // my discriminator
'Lioness100#4566'; // my tag
There are a few ways to mention someone in a message.有几种方法可以在消息中提及某人。 The first one is the built-in
Message.reply()
function which will send the given message, prepended with <mention of author>,
第一个是内置的
Message.reply()
函数,它将发送给定的消息,前面带有<mention of author>,
The second method would be converting the User
/ GuildMember
object to a string.第二种方法是将
User
/ GuildMember
对象转换为字符串。 Discord will automatically parse it as a mention. Discord 会自动将其解析为提及。
The third and most versatile method would be using mention syntax.第三种也是最通用的方法是使用提及语法。 For users, it's
<@id>
.对于用户,它是
<@id>
。
I'm not quite sure if something is wrong with your code, or what exactly your asking, but I think you want channels to be dynamically chosen per message.我不太确定您的代码是否有问题,或者您的要求是什么,但我认为您希望按消息动态选择频道。 You can do this by creating an
args
variable.您可以通过创建一个
args
变量来做到这一点。
// new usage:
// {prefix}say channelName message
client.on('message', function(message) {
if (message.author.bot) return;
// split string by every space => returns an array
const args = message.content.slice(prefix.length).split(/ +/);
// removes the first element (the command name) and stores it in this variable => returns a string
const command = args.shift().toLowerCase();
// now it's much easier to check the command
if (command === 'say') {
// the first element of `args` is the first word after the command, which can be the channel name
// we can then find a channel by that name => returns a Channel or undefined
const channel = message.guild.channels.cache.find(
(c) => c.name === args[0]
);
if (!channel)
return message.channel.send(`There is no channel with the name ${args[0]}`);
// join all args besides the first by a space and send it => returns a string
channel.send(args.slice(1).join(' '));
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.