So I am working on my bot, and for my ;say command I have the following code. The issue is, that when a user does something like ";say hi" it repeats as ;say on one line, goes down a line, and on that line it says hi. Before I added in a command handler, the command worked fine, however only now am I running into issues.
The code (including vars + command handler portion) -
const Prefix = require('./../../config.json');
module.exports = {
name: "say",
description: "Says the user input",
execute (Client, message, Args) {
let Say = message.content.substring(({Prefix} + 'say').Args).split(" ");
let Output = Say.splice(1);
message.channel.send(Output);
message.delete({ timeout: 1 });
},
};
in case it comes of necessity for some reason, my config.json file (might be an issue with the prefix) is in the "container" directory in the below file location -
container/commands/fun/say.js
At first, why don't you use message.content.substring(`${Prefix}say`); instead of these two lines
let Say = message.content.substring(({Prefix} + 'say').Args).split(" ");
let Output = Say.splice(1);
that would be the same, no?
I also think if you have a problem like this it's probably because there's something missing in the event handling part of your code.
Also you need to select the prefix from your config.json file, by adding .prefix or the name of your variable in your .json file after the require('./../../config.json'); not to get the entire object.
Related
I'm working on a giveaway bot and got that error. This is the full error
(node:4) UnhandledPromiseRejectionWarning: SyntaxError: The storage file is not properly formatted (Unexpected end of JSON input).
at GiveawaysManager.getAllGiveaways (/app/node_modules/discord-giveaways/src/Manager.js:308:27)
at async GiveawaysManager._init (/app/node_modules/discord-giveaways/src/Manager.js:391:30)
Here is my code:
const { GiveawaysManager } = require("discord-giveaways");
const manager = new GiveawaysManager(bot, {
storage: "./giveaways.json",
updateCountdownEvery: 10000,
default: {
botsCanWin: false,
embedColor: "#FF0000",
reaction: "🎉"
}
})
bot.giveawaysManager = manager;
}
})
I'm new to coding so it wil be great if you explain in baby steps
I had this same issue, and it turns out that the giveaways.json file only had a [] in it.
It is best if you just don't add a file because the module should add one for you!
The storage file is your ./giveaways.json. As seen in the npm documentation, it saves the file in JSON format which is highly likely to go wrong, make sure that you haven't touched the giveaways.json file, much less change it. Adding even a single line in the giveaways.json file may cause the GiveawaysManager to append wrongly and not be able to read it.
My suggestion is basically delete the ./giveaways.json file. This should refresh the file and be rid of all syntax errors unless it was from the npm module itself. Note that deleting it, will delete and therefore, stop all the giveaways in process, so make sure you have no giveaways in progress.
If this doesn't fix the issue, then delete the ./giveaways.json file again, and create a new ./giveaways.json file with this as it's contents:
{}
i tried to add a command handler, but command files are showing incorrect values, for example the bot's ping is showing up as NaN and uptime/users/servers are all showing as zeros. the command was working fine until i moved it into it's own command file. i'm not sure what the problem is as no errors occur in the console. please help, the code to my index.js and one command file is below
my best guess as to where the issue is:
module.exports = {
name: bot,
description: displays bot information.,
execute(message, args) {
//code
}
https://pastebin.com/hJ4nessW
It looks like your bot.js file has some lines without using Discord.js's Embed class...
At the first part where's
const Discord = require(`discord.js`);
const bot = new Discord.Client(); // <- here
isn't necessary because it's defined in index.js, so should be use from index.js
switch (args[0]) {
case `ping`:
bot.commands.get(`ping`).execute(message, args, bot /* use it */ );
break;
case `bot`:
bot.commands.get(`bot`).execute(message, args, bot /* use it */ );
break;
}
and bot.js
execute(message, args, bot) {
// You can use bot variable
// ...
I recommend to avoid spaghetti code, because it looks worse and spends a lot of time... I made the shorten, less lines and working code version -> https://pastebin.com/sRNacWYt
I thought that allowing people to turn my blacklist on and off for their server would be kinda neat, but I didn't find much success so far. Also I was wondering, is it hard to make a command that allows people to add their own words to the blacklist? Here's the code:
let blacklisted = ['bad words here']
let foundInText = false;
for (var i in blacklisted) {
if (message.content.toLowerCase().includes(blacklisted[i].toLowerCase())) foundInText = true;
}
if (foundInText) {
message.delete();
}
});
You could use a variable for turning on or off the blacklist. BacklistOn = true, And use a if statement before the code you copied in here. Then make a command that changes that variable. You would need to store the variable in a JSON file if you want the setting to be saved when restarting the bot or if the bot crashes. Tutorial on reading/writing to JSON with node.js https://medium.com/#osiolabs/read-write-json-files-with-node-js-92d03cc82824
I'm having an issue in dart/flutter, I want to check if the user opens a file and modified/edited the file, and only if this happens I do something that is not important... for that purpose I used this chunk of code:
String dir = await bloc.buildFileDirectory(filePath);
File file = new File('$dir/filename');
DateTime date = await file.lastModified();
if(date.millisecondsSinceEpoch < someOtherFileTimeStamp)
do something
with the method lastModified() from File class in Dart, when the user open the file, it will consider a modification, and so it will trigger the if condition... is there any functional way to check for really modifications on the file? like editing some text inside the file, or change the name...something like that
Ty for your time
Yes, you can use the watcher library's FileWatcher.
Here's an example on how to use it:
final watcher = FileWatcher('main.dart');
final subscription = watcher.events.listen((event) {
switch (event.type) {
case ChangeType.ADD:
print('Added file');
break;
case ChangeType.MODIFY:
print('Modified');
break;
case ChangeType.REMOVE:
print('Removed');
}
});
If you need to determine whether the file changed since last time your program ran, then this will of course not work and you will have to use the timestamp as you're doing, or even hash the contents of the file and store it somewhere, so you can check later if the hash is still the same.
I followed this answer and it looks almost the thing I need.
The problem there is that he already knows the filename and I am doing e2e test for downloading a file, but the filename depends on the current time (even with milliseconds) so I don't really know the name (or it would be very difficult to get it).
I think I am missing something very simple here, but I was thinking of two ways:
Recreate filenames (with the same function that returns the name of this file) and start checking for existance of a file with that name, if it doesn't exist, then move to the next millisecond until I hit the right name.
Check the download folder for existance of "any" file, if I find one there then it should be the file I am downloading (for this case I don't know how to check an entire folder in protractor).
Hope you guys could help with these alternatives (I would like some help with point 2) or maybe give me a better one. Thanks
I ended up following #alecxe's suggestion and here is my answer:
var glob = require("glob");
browser.driver.wait(function () {
var filesArray = glob.sync(filePattern);
if (typeof filesArray !== 'undefined' && filesArray.length > 0) {
// this check is necessary because `glob.sync` can return
// an empty list, which will be considered as a valid output
// making the wait to end.
return filesArray;
}
}, timeout).then(function (filesArray) {
var filename = filesArray[0];
// now we have the filename and can do whatever we want
});
Just to add a little bit more background information to the #elRuLL's answer.
The main idea is based on 2 things:
browser.wait() fits the problem perfectly - it would execute a function continuously until it evaluates to true or a timeout is reached. And, the timeout mechanism is already built-in.
glob module provides a way to look for filenames matching a certain pattern (in the worst case, you can wait for the *.* - basically, any file to appear)