I've coded a "say mand" that's supposed to execute a message every time I type in -say.
It's working fine, I just want the prefix to be set to "?" instead of "-" only for that one mand, the other ones are supposed to be set to the main one ("-"). On top of that I want it to delete the mand after typing it in, so all that remains is the message [(e.g ?say hello --> (delete "?say hello" --> send message "hello" to text channel)].
I also want to specify the text channel in my mand, instead of just setting it to send the messages only to one specific channel [e.g -say (message) (text channel)]
It would also be pretty cool if it said something like "Done." and deleting that confirmation after ~5 sec.
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've coded a "say mand" that's supposed to execute a message every time I type in -say.
It's working fine, I just want the prefix to be set to "?" instead of "-" only for that one mand, the other ones are supposed to be set to the main one ("-"). On top of that I want it to delete the mand after typing it in, so all that remains is the message [(e.g ?say hello --> (delete "?say hello" --> send message "hello" to text channel)].
I also want to specify the text channel in my mand, instead of just setting it to send the messages only to one specific channel [e.g -say (message) (text channel)]
It would also be pretty cool if it said something like "Done." and deleting that confirmation after ~5 sec.
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!
Share Improve this question edited Oct 23, 2020 at 20:18 Deazy asked Oct 23, 2020 at 20:12 DeazyDeazy 111 gold badge2 silver badges5 bronze badges 1- 1 You seem to be asking for someone to write some code for you. Stack Overflow is a question and answer site, not a code-writing service. Please see here to learn how to write effective questions. – Lioness100 Commented Oct 24, 2020 at 1:49
4 Answers
Reset to default 1I 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 mand.
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 mend would be like
!say #generalTextChannel abc
where #generalTextChannel is tagging the channel
or
!say 767374258492932106 abc
where 767374258492932106 is the channel 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 mand, you can just use the other line you used without ['?'].every
Use just this: if (!message.content.startsWith(prefix) || message.author.bot) return;
.
Then check for each mand individually under this. if (mand == 'say')
A very simple way of getting arguments:
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const mand = args.shift().toLowerCase();
The say mand would look something like this:
if(mand == 'say'){
//then you could find the channel
//https://discord.js/#/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.
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 mand = args.shift().toLowerCase();
if (mand === '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 mand\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 mand = args.shift().toLowerCase();
//bla bla bla (the rest of the mands, etc.)
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744962357a4603465.html
评论列表(0条)