if(args[1]=='help'){msg.channel.send('`.help`: sends command')}else if(args[1]=='credits'){msg.channel.send('`.credits`: sends credits')}else if(args[1]=='server'){msg.channel.send('`.serverinfo`: sends server info')}else if(args[1]=='botinvite'){msg.channel.send('`.botinvite`: sends bot invite')}else if(args[1]=='pin'){msg.channel.send('`.pin`: pins message (admin only)')}else if(args[1]=='prefix'){msg.channel.send('`.prefix`: sends prefix')}else if(args[1]=='random'){msg.channel.send('`.random {letter, number, string, color} {0x, hex shorthex, rgb (color only)}`: sends random letter, number, string or color, 0x, hex, shorthex, rgb is only for color')}else if(args[1]=='uwu'){msg.channel.send('`.uwu`: uwuifies text')}else if(args[1]=='owo'){msg.channel.send('`.owo`: owofies text (basically a better .uwu)')}else if(args[1]=='dog'){msg.channel.send('`.dog`: sends random dog photo')}else if(args[1]=='urban'){msg.channel.send('`.urban {word}`: searches word up on urban dictionary and sends the definition')}else if(args[1]=='cat'){msg.channel.send('`.cat`: sends random cat photo')}else if(args[1]=='embed'){msg.channel.send('`.embed -{title} -{description} -{hex color}`: sends a embed (make sure to add - in front of title, description and color)')}else if(args[1]=='dm'){msg.channel.send("`.dm {text}`: dm's message author text")}else if(args[1]=='coin'){msg.channel.send('`.coin`: flips a coin')}else if(args[1]=='avatar'){msg.channel.send("`.avatar {mention}`: sends mentioned user's avatar")}else if(args[1]=='ping'){msg.channel.send('`.ping`: sends bot ping')}else if(args[1]=='nick'){msg.channel.send('`.nick {nickname}`: set your nickname (only for roles under bot role)')}else if(args[1]=='neko'){msg.channel.send('`.neko`: sends random neko pic')}else if(args[1]=='delete'){msg.channel.send('`.delete`: deletes the message')}else if(args[1]=='fox'){msg.channel.send('`.fox`: sends random fox pic')}else if(args[1]=='bird'){msg.channel.send('`.bird`: sends random bird pic')}else if(args[1]=='duck'){msg.channel.send('`.duck`: sends random duck pic')}else if(args[1]=='seal'){msg.channel.send('`.seal`: sends random seal pic')}else if(args[1]=='spoilers'){msg.channel.send('`.spoilers`: hides spoiler message')}else if(args[1]=='useless'){msg.channel.send('`.useless`: sends random useless website')}else if(args[1]=='servericon'){msg.channel.send('`.servericon`: sends server icon')}else if(args[1]=='choose'){msg.channel.send('`.choose {option} {option}...`: helps you decide')}else if(args[1]=='word'){msg.channel.send('`.word {number of words}`: sends random word(s) (cap is 5)')}else if(args[1]=='proglang'){msg.channel.send('`.proglang`: sends random programming language')}else if(args[1]=='panda'){msg.channel.send('`.panda`: sends random panda pic')}else if(args[1]=='lyrics'){msg.channel.send('`.lyrics {song name}`: sends lyrics of song name')}else if(args[1]=='userinfo'){msg.channel.send('`.userinfo {mention}`: sends user info')}else if(args[1]=='binary'){msg.channel.send('`.binary {encode/decode} {text/binary}`: encodes text to binary or decode binary to text')}else if(args[1]=='base64'){msg.channel.send('`.base64 {encode/decode} {text/base64}`: encodes text to base64 or decode base64 to text')}else if(args[1]=='reverse'){msg.channel.send('`.reverse {text}`: reverses text')}else if(args[1]=='stickbug'){msg.channel.send('`.stickbug`: send random stickbug meme')}else if(args[1]=='shiba'){msg.channel.send('`.shiba`: sends random shiba inu pic')}else if(args[1]=='mlem'){msg.channel.send('`.mlem`: sends random mlem pic')}else if(args[1]=='nugget'){msg.channel.send('`.nugget`: sends random nugget pic')}else if(args[1]=='earthporn'){msg.channel.send('`.earthporn`: sends random r/earthporn pic')}else if(args[1]=='meme'){msg.channel.send('`.meme`: sends random meme (probably')}else if(args[1]=='food'){msg.channel.send('`.food`: sends random r/foodporn pic')}else if(args[1]=='calculate'){msg.channel.send('`.calculate {calulation}`: calculates calculation')}else if(args[1]=='pasta'){msg.channel.send('`.pasta`: sends random r/pasta pic')}else if(args[1]=='dice'){msg.channel.send('`.dice {d4, d6, d8, d10, d12, d20, d30, d100}`: rolls a dice')}else if(args[1]=='8ball'){msg.channel.send('`.8ball {question}`: ask a question to the 8ball')}else if(args[1]=='tinyurl'){msg.channel.send('`.tinyurl {url}`: shortens a link via tinyurl')}else if(args[1]=='rndtranslate'){msg.channel.send('`.rndtranslate {text}`: translates text into too many languages')}else if(args[1]=='kick'){msg.channel.send('`.kick {mention} {reason}`: kicks mention (admin only)')}else if(args[1]=='hug'){msg.channel.send('`.hug`: sends random anime hug gif')}else if(args[1]=='koala'){msg.channel.send('`.koala`: sends random koala pic')}else if(args[1]=='wink'){msg.channel.send('`.wink`: sends random anime wink gif')}else if(args[1]=='pat'){msg.channel.send('`.pat`: sends random anime pat gif')}else if(args[1]=='rps'){msg.channel.send('`.rps {rock, paper, scissors}`: play a game of rock paper scissors with the bot')}else if(args[1]=='japan'){msg.channel.send('`.japan`: sends random r/japanpics pic')}else if(args[1]=='poll'){msg.channel.send('`.poll {channel} {question}`: creates a poll in channel (admin only)')}else if(args[1]=='hastebin'){msg.channel.send('`.hastebin {text}`: creates a hastebin')}else if(args[1]=='emojitext'){msg.channel.send('`.emojitext {text}`: translates text to discord emojis')}else if(args[1]=='translate'){msg.channel.send('`.translate {language} {text}`: translates text to language do .translatelangs for supported languages')}else if(args[1]=='translangs'){msg.channel.send('`.translangs`: sends list of supported languages for .translate')}else if(args[1]=='covid19'){msg.channel.send('`.covid19`: sends covid19 statistics')}else if(args[1]=='slowmode'){msg.channel.send('`.slowmode {channel} {seconds}`: sets channel slowmode to seconds')}else{if(args[1]){msg.channel.send('Invalid command.')}}
You think too small. Why give them anything they either get the random page or we just put them on a endless loading page or we just hit them with with a 403 error. Why give stuff away. Save that for the next chump who may pay for it.
I don’t really like discord bots that do basic things like pinning messages or adding emojis. I can already do those things on my own. I don’t need a bot to create a channel. Yet, I always see these bots on preteen servers
I’d say it depends on the context and requirements for maintaing it. A resource file of some sort would propably do just fine, but that would require a dev to maintain it (CRUD operations) and that may do just fine if there are no frequent change) otherwise a database. But i’d say go with what feels better, as long as it is not this 😬
Which will be converted to a javascript object - turns out that is in fact the same as a dictionary 😉 in java and .net i would believe an abstraction off a json file would be a dictionary as well.
Many languages many names like dictionary, object, map etc. but what you are looking for is essentially something which holds a value for each unique key. A key-value pair. Since what you are doing here is checking if something exists and then getting an output for if something exists, then what you can do is this -
Doing it this way, your logic of how you want handle the code and send responses is at once place. I am not sure if discord bot handles this kind of case or not but this is something you might want to add in other applications where you want to check if what you sent was delivered successfully or not which you can do here very easily at just one place.
Another advantage is that you don't need to have this responses object in your code at all. You can fetch this from an external database or idk an csv file I guess. You can give access to this database to a different person to handle this stuff without having to give that person access to the code. Basically making the entire thing scale-able
it doesn't really improve the code though. instead of many else-ifs, you get many cases. I'd suggest a map/dictionary and use an O(1) get operation. not only is the resulting code much shorter, it's also faster (not that it's relevant in this case)
maybe I should've been more specific, because HashMaps have constant complexity (I just don't know any other map implementations)
if you consider each case in a switch an item of a collection, it would be O(n) complexity, since in the worst case you have to check all cases. I think technically it would still be O(1), but my point is you have to check each case until you find a match
As a potential devil's advocate, wouldn't a map probably not really be that much faster, because instead of having to check 30 booleans, you have to load 30 description strings all into memory before executing the O(1) operation, and since it is an interpreted language the overhead may be about the same?
Or is parsing the syntax of if... else a much harder operation than just loading strings into a map?
I'm pretty sure it doesn't make any significant difference if you do it once, but I'm assuming this method will be called every time a command needs to be executed
as far as I know, string comparisons are somewhat expensive (if there's any other way you can do it). at least it's more expensive than hashing the string und getting a value from a hash map
It's generally easier to add entries into a JSON file than to make changes to the code base. Rather than continually expanding a switch or if/else, the keywords / responses could be contained in a separate file completely. If you ever want new entries you can just update that one file on the fly, and you have far, far less code to manage.
And how would yo manage the "msg.channel.send('Something')"?? Would you look in the json for the key containing the name of the args[1] and then using its value?
// load your json somewhere in the start of the bot
const commandsHelp = loadHelpCommandMessages();
// where you are parsing your command
const helpMessage = commandsHelp[args[1]] ?? "Wrong command.";
msg.channel.send(helpMessage);
Trying to access a key that doesn't exist in the JSON would give you undefined, so you can validate it with the null coalescing operator (??) to give the error message if the key doesn't exist.
Alternatively, you can also use a Map and/or read it from other data formats, but I think that loading/saving it in JSON format would be the best approach for readability reasons (and working with already JS-native solutions).
By using a bunch of if..else, or a switch case, not only is the code ugly to read, but it'll also be slow.
If the condition you need to hit is written at the end of your series of iterations, you still need to go through all the previous ones.
If you use a map, you iterate once over your list of arguments to build your map; and then you can replace all your conditions with retrieving the value from your map. Depending on which language you're using, you can also handle cases where your key doesn't exist
It is. It is just way of storing responses, not deciding what response to get.
You can just set entries in map that would come from the json file. Why would you? Idk, not to clutter actual code, make it more user replaceable, to use i18n.
I think the point is that JSON is easily human readable as a dictionary. Sure, any data format will work, but JSON is easier to work with (and probably simpler to parse into a dictionary with existing libraries)
Why just not go with a JSON lol, way easier, escalable and faster.
Y’all are interpreting this message in a different way than a new dev would. The data format itself doesn’t help improve the above code in anyway if the dev doesn’t know about maps. Yes JSON is easily parsed into a map. But the format itself gives no indication to a new dev, so no JSON is not the answer.
I was proposing a possible solution to fix the readability. From that point onwards, the dev is the one that would take care of the implementation, as this could (and should) be simplified a lot.
I'm not sure about the JSON vs Map thing, as both are technically treated as hash maps. Both are pretty fast, but Maps will perform better when you have to constantly add and remove keys.
Okay I have a question, I have a JSON object and I need to parse and store the values of it inside of a struct/object (language is C++). Values can be any type so I can't use a map here. I'm really afraid I'm gonna have to ended up writing something like this... So if any of you guys have any suggestion I'd be really grateful, thanks.
Hmm, I'm using nlohmann's json lib to parse json but actual problem is the json objects I have doesn't contain every member of the struct most of the time (i retrieve them from aws iot core's shadow delta event, if you're familiar with that). I'll read the comments and check the libs in more detail tomorrow at work, maybe I can find something that works for me, thanks again!
Edit: Oh just realized I wrote my previous comment like I have only one static json object, sorry for that too :')
Making a command handler isn’t that hard, people read page one of the discord.js docs and instantly make a bot like this, even a switch would be horrible for this implementation. Just use fs and an array it takes two seconds. People piss me off
from my abandoned discord bot: I have separate command files with all the metadata and code for that command in each file. It’s all stored in objects. This makes it easy to modify a command without having to go to like 4 different files to change stuff.
Yeh, this is the way I would approach it. Have methods like matches() (to test against the incomming message), help() (to return an object that describes the match, additional arguments, a description and extended help), and run() in order to execute the command against the incomming message.
Load those into an array, when a message comes in, iterate through the array testing each command's matches() with the message. When a command passes the match, run the command then exit the loop.
You didn’t even use a synonym you used the correct term, a “dictionary” is just a python hash map which makes it ambiguous when not talking about python.
It's the same thing. Map, dictionary, hash table, associative array, table, object (sometimes)... they're all just different names for the same concept.
171
u/sertdfyguhi Sep 07 '21
Code:
123
u/[deleted] Sep 07 '21 edited Jul 04 '23
fuck u/spez
87
u/EquivalentOne9996 Sep 07 '21
Use a dictionary damnit
30
u/SaucyParamecium Sep 07 '21
uwu
15
u/Hirogen_ Sep 07 '21
Maybe someone should tell him what a dictionary is and how to use it
6
u/champwolverine Sep 07 '21
Sooo...whats the better way or quicker way to do this?. Except Switch case
4
u/Jackiboi307 Sep 07 '21
r/yanderetechnique
2
u/inventord Sep 07 '21
This looks like a discord bot
2
u/CSsharpGO evil Sep 07 '21
Cleanest and most inspired discord bot
4
u/EnanoForro Sep 07 '21 edited Sep 07 '21
Why not just... switch(args[1]) ?
lol getting negatives for a question, I'm definitely appearing in one of this posts sooner or later xD
1
u/gothcow5 Sep 07 '21
Else if
1
u/TheGamer456YT Sep 07 '21
I used to do this, but then I switched to something better than switch case that for the love of god I can't remember!
1
u/ColourAttila Sep 07 '21
Oh yes! The professional discord bot developers
1
u/Yangishrobin Sep 07 '21
This is contributing to the fact that we are doomed as a species.
1
u/oNamelessWonder Sep 07 '21
Okay I have a question, I have a JSON object and I need to parse and store the values of it inside of a struct/object (language is C++). Values can be any type so I can't use a map here. I'm really afraid I'm gonna have to ended up writing something like this... So if any of you guys have any suggestion I'd be really grateful, thanks.
1
u/Character_Medical Sep 08 '21
And all I'm wondering is who hurt this guy in childhood 🤔
1
u/weflown Sep 08 '21
pov: yandere dev every time on this subreddit
1
u/[deleted] Sep 08 '21
Thats so painful...I couldn't do much better but damn this hurts to look at.
1
u/Goplaydiabotical Sep 27 '21
Um, how about a hashtable lookup there my guy?
1
u/Bortan Dec 18 '21
for the love of code use a dynamic help command
0
u/Minteck Sep 07 '21
I wonder if there's a simple way to do this... Hum, maybe
case...0
u/TesAlt Sep 07 '21
Making a command handler isn’t that hard, people read page one of the discord.js docs and instantly make a bot like this, even a switch would be horrible for this implementation. Just use fs and an array it takes two seconds. People piss me off
-2
u/[deleted] Sep 07 '21
Switch?
62
u/[deleted] Sep 07 '21
[removed] — view removed comment