I've seen a lot of people asking the same thing on Stack Overflow but I didn't see any of the cases in which people use the same type of code as me, like for instance I cannot use .toLowerCase().
if (message.substring(0, 1) == '+') {
var args = message.substring(1).split(' ');
var cmd = args[0];
args = args.splice(1);
switch(cmd) {
// Help command
case 'HELP':
bot.sendMessage({
to: channelID,
message: commandList
});
break;
Try putting toUpperCase() here
var cmd = args[0].toUpperCase();
or here:
switch(cmd.toUpperCase()) {
And if youre getting an error saying toUpperCase cant be put on undefined, then your code is broken somewhere here:
var args = message.substring(1).split(' ');
var cmd = args[0];
so try and see if your message is actually what you think it is.
Related
module.exports = {
config: {
name: "help me",
noalias: ""
},
run: async (bot, message, args) => {
}
}
I have this piece of code however when I run the command it does not do anything (There's code inside the run: async... I removed it to keep it short.) When I do !!help me, !!help, or !!me it does not do anything. The code below this message will be my message.js file.\
const { PREFIX } = require('../../config.json');
module.exports = async (bot, message) => {
let args = message.content.slice(PREFIX.length).trim().split(/ +/g);
let cmd = args.shift().toLowerCase();
if (!message.content.startsWith(PREFIX)) return;
var commandfile = bot.commands.get(cmd) || bot.commands.get(bot.aliases.get(cmd))
if (commandfile) commandfile.run(bot, message, args);
}
Discord.JS Version: 12.2.0
That's because you're removing the first argument when you do args.shift(). Don't do that. It's taking in the command name as just "help", and "me" as args[0], which isn't bad, but doesn't work with what you're trying to do.
Instead, make it take in the argument at index position 0 and use that as the command name:
let args = message.content.substring(PREFIX.length).split(/ +/g);
let cmd = args[0];
You shouldn't have multiple words as a command name. Make it helpme, help or perhaps help-me, otherwise properly handling user input will be made unnecessarily difficult. This is why it didn't do anything, since the command name was help me but it interpreted the command as just help.
Looking at your code makes me think you'd be better off matching the commands using regex like github/telebot does. Here is a short example how this could look like:
const watchRegExp = /^\/watch ([a-z0-9]{64}) ([0-9]*) ?(.*)$/;
// let's say you sent: /watch 81a40da1f3e110017a6982e9e354577926ac047f57a954f70f365e8f5ae65ac6 5 cocacola
bot.on(watchRegExp, (msg, props) => {
const txid = props.match[1]; // '81a40da1f3e110017a6982e9e354577926ac047f57a954f70f365e8f5ae65ac6'
const threshold = props.match[2]; // 5
const label = props.match[3]; // 'cocacola'
// do your stuff here
}
Hope this helps a bit,
Cheers
When im coding the part of the discord bot that gives a role when activated i keep getting this error and im not sure if there is a bug i dont see but if you see it pls help me correct it!
Error message.
if(mesage.content.startsWith(prefix + "prune")){
^
ReferenceError: mesage is not defined
Section of script with problem.
if(mesage.content.startsWith(prefix + "prune")){
let args = Message.content.split(" ").slice(1);
let author = Message.member;
let role = message.guilds.roles.find('name', "Moderator");
if(author.roles.has(role.id)){
if(!args[0]){
Message.delete();
Message.author.send("No arguments given.");
return;
}
}
}
Full Script
const Discord = require('discord.js')
const Client = new Discord.Client
const prefix = "/";
Client.on('ready', ()=>{
console.log('Bot is online.');
})
Client.on('message', (Message)=>{
if(!Message.content.startsWith(prefix)) return;
if(Message.content.startsWith(prefix + "hello")){
Message.channel.send("Hello.");
}
if(Message.content.startsWith(prefix + "help")){
Message.channel.send("The only avaible command right now is /help and /hello.")
Message.author.send("This is only for test purposes!");
}
if(mesage.content.startsWith(prefix + "prune")){
let args = Message.content.split(" ").slice(1);
let author = Message.member;
let role = message.guilds.roles.find('name', "Moderator");
if(author.roles.has(role.id)){
if(!args[0]){
Message.delete();
Message.author.send("No arguments given.");
return;
}
}
}
})
Client.login("<Bot Token>");
All your code refers to Message, except this line which is mesage, not only mis-spelled, but incorrectly lower-case.
Making that consistent with the other things should fix the issue.
Note that JavaScript generally reserves capital letters for things like classes, lower-case for variables and arguments. As you can see here the syntax highlighter thinks this is a class and is colouring it accordingly. Lower-case message is the conventional argument name.
Change mesage (the typo) with message (not uppercase). Make sure to also change Message with message (again, no uppercase)
So, guys, I am trying to change variables in discord.js
what I am making is like that
var Version = "10"
bot.on(bot.on("message", message => {
case 'Test'
messages.channel.send(Version)
break;
})
What I am trying to make is
Change the Version with a command example of a command
+edit Version 100
And the version variable will be changed and when you type test
it will say 100 not gonna say 10
I can tell you are very new to coding in javascript. I have written out some starter code for you to help you out.
Not everyone will do this for you and I rarely do it as well. We do this because we believe that offering the quick and dirty solution teaches people nothing.
Therefore I would appreciate that you take a look at these links below to learn about why my code does what it does. :)
https://www.w3schools.com/js/js_variables.asp
https://www.w3schools.com/jsref/jsref_split.asp
https://www.w3schools.com/js/js_switch.asp
https://www.w3schools.com/js/js_arrays.asp
var version = "10";
bot.on("message", message => {
let args = message.split(" ");
if(args.length === 0) {
//send error no arguments
return;
}
switch(args[0]) {
case 'test':
messages.channel.send(version.toString());
break;
case 'version':
if(args.length < 2) {
//send error too short message
} else {
version = args[1];
}
break;
default:
//send not found
break;
}
});
With Discord.io I'm trying to send a message to a different channel than what it is sent from, but after a lot of Googling and experimenting I still can't seem to find a way to do so.
I tried to simply change the channelID to the channel I want it to send to and overide the channelID to the channel I want it to send to, but with no success
For example:
bot.on('message', function (user, userID, channelID, message, evt) {
if (message.substring(0, 1) == '!') {
var args = message.substring(1).split(' ');
var cmd = args[0].toLowerCase();
args = args.splice(1);
switch(cmd) {
case 'test':
bot.sendMessage({
to: // channel id here,
message: 'If all went well this got sent in another channel'
});
break;
default:
bot.sendMessage({
to: channelID,
message: 'I wasn\'t able to understand this command, please try again...'
});
break;
}
}
});
Hopefully you can help me out.
If there is something not clear for you, feel free to ask.
Thanks in advance.
So I found out why it didn't work, in the documentation it says channelID is Snowflake. What I didn't know is that this looks a lot like just a regular string. So when I tried to overide channelID by a string, it seemed to work. Like this:
bot.on('message', function (user, userID, channelID, message, evt) {
if (message.substring(0, 1) == '!') {
var args = message.substring(1).split(' ');
var cmd = args[0].toLowerCase();
args = args.splice(1);
switch(cmd) {
case 'test':
bot.sendMessage({
to: 'your channel id here',
message: 'If all went well this got sent in another channel'
});
break;
default:
bot.sendMessage({
to: channelID,
message: 'I wasn\'t able to understand this command, please try again...'
});
break;
}
}
});
I have never coded a Discord Bot before but am familiar with Javascript so I figured I would give it a shot. I used the beginner files from this site Digital Trends but am running into some issues.
I have the bot running in my server and the basic command swapped from "!" to "?" and the included command "?ping" does return the expected response "Pong!"
I run a server where we start a video game each month and play through while discussing it along the way, similar to a book club. So I'm trying to create a channel where people can suggest a game using a bot command since I don't trust they could handle following simple rules on their own.
What I'm trying to figure out is how to go about taking a user command of:
?gs "Video Game Title" "Platform"
And having the bot delete the command and repost as:
#user suggested Video Game Title for Platform
While also adding reaction emojis "👍" and "👎" to allow other users to vote.
I'm not asking for anyone to do this for me, but to simply help point me in the right direction of how to code this with Discord in mind using JS (if possible)
Here is my current "bot.js" code:
var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, {
colorize: true
});
logger.level = 'debug';
// Initialize Discord Bot
var bot = new Discord.Client({
token: auth.token,
autorun: true
});
bot.on('ready', function (evt) {
logger.info('Connected');
logger.info('Logged in as: ');
logger.info(bot.username + ' - (' + bot.id + ')');
});
bot.on('message', function (user, userID, channelID, message, evt) {
// Our bot needs to know if it will execute a command
// It will listen for messages that will start with `!`
if (message.substring(0, 1) == '?') {
var args = message.substring(1).split(' ');
var cmd = args[0];
args = args.splice(1);
switch(cmd) {
// !ping
case 'ping':
bot.sendMessage({
to: channelID,
message: 'Pong!'
});
break;
// Just add any case commands if you want to..
}
}
});
args = args.splice(1);
This line is incorrect, args (being a string) has no .splice() method. (I often get .slice() and .split() confused, so this happens to me a lot too!)
Instead, use:
args = args.split(" ").slice(1);