问题
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.
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!"
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. 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!
回答1:
First problem
User.tag doesn't actually mention the user. A user's tag in a combination of their username and 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>,
The second method would be converting the User/GuildMember object to a string. Discord will automatically parse it as a mention.
The third and most versatile method would be using mention syntax. For users, it's <@id>
.
- All mention syntax
Your second problem
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.
// 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(' '));
}
});
- Learn about command handling
来源:https://stackoverflow.com/questions/64433639/how-to-make-a-discord-js-bot-say-custom-messages-in-a-specific-channel-tag-a-u