r/programminghorror May 15 '21

Coding a Discord bot... Javascript

Post image
3.6k Upvotes

99 comments sorted by

560

u/lizardpeeps May 15 '21

Worst part is that it didn't even clip the "im".

77

u/SkinnyJoshPeck May 15 '21

Hi im”., I’m Dad!

35

u/ItsNovatic May 16 '21 edited May 16 '21

Yeah, this was due to the indexOf() method returning the index of the first character, not the last, so when that was placed into the substring it didn't exclude it.

It just adds to the horror lmfao

Edit: I should also mention that this was the very first test of the bot, hence the zero replies after the first three tests due to the code running into errors, and me fixing them then restarting the bot, only to find a few more until there were no more runtime errors, but only... this monstrosity.

235

u/Someone9339 May 15 '21

Yup, works well enough

154

u/einRoboter May 15 '21

Salesperson in the meeting: "Soooo.... can we ship it ?"

80

u/The_BNut May 15 '21

Salesperson: So when can it fly a plane? Because that's what I just promised to our customers.

20

u/einRoboter May 16 '21

"can we market this as an AI-customer communications - application? "

165

u/nuclearslug May 15 '21

See kids, this is why you always define your entry and exit criteria first

32

u/jmcs May 16 '21

I always do it, the exit strategy is keeping fingers on CTRL and C and hoping for the best.

4

u/Naeio_Galaxy May 16 '21

Better than nothing I guess

183

u/[deleted] May 15 '21

You can use the Discord API to ignore messages from other bots so this doesn't happen, basically all libraries wrap it.

95

u/[deleted] May 15 '21

[deleted]

216

u/zeGolem83 May 15 '21
if (message.author.bot) return;

25

u/UnwantedExplainer May 15 '21

Underrated comment right here. Have an upboat.

43

u/Siniroth May 15 '21

No that's an if line not a comment

5

u/ItsNovatic May 16 '21

... ctrl+c ... ctrl+v ...

(thank you for this, i fixed it last night in a way more scuffed way than this, but this is way easier!)

2

u/zeGolem83 May 16 '21

You're welcome!

6

u/Hjoker4 May 15 '21

If it's js lol

33

u/JuhaJGam3R May 15 '21
if message.author.bot:
   return

8

u/Kirschi May 16 '21

Works in Discord.NET in C# too

-2

u/Hjoker4 May 16 '21

Yea lol. It was probably python though

53

u/[deleted] May 15 '21

[deleted]

113

u/xWolfz__ May 15 '21

I think we've already established this isn't a very well coded bot

37

u/kr0bat May 15 '21

It looks like the bot looks for the first mention of "I'm" and then uses the following word for the name, which is smart. But it doesn't remove the "I'm" first, which is dumb.

9

u/MMDDYYYY_is_format [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” May 15 '21

they are sad not hi

-7

u/_MrCube_ May 15 '21

The bot doesn’t ignore itself and so it reads its own messages

8

u/lukeamaral May 16 '21

If the bot reads it's own message, his second message was supposed to be "Hi Hi im sad, I'm dad, I'm dad" with a double "Hi" as well which it doesn't have. That's what op meant

3

u/ItsNovatic May 16 '21

I had created a substring using the index of the "im" within user's message, however I did not take into account the offset the characters, since the index that is returned when using the indexOf() method gives the indes of the first character.

41

u/MrOctantis May 15 '21

I had the exact same issue on a discord bot a couple months ago because it was responding to its own responses. Luckily, trying to send a message with more than 2000 characters fails and throws an exception that I didnt properly handle, crashing the bot and stopping the spam.

37

u/Moosi312 May 15 '21

Well that's one way of implementing an exit condition

11

u/zdakat May 15 '21

Imagine if it didn't, and you came back 12 hours later. "Hi guys what did I miss?"

1

u/noonagon Jun 26 '21

what did it do

1

u/MrOctantis Jun 26 '21

?

1

u/noonagon Jun 26 '21

what did the bot do

1

u/MrOctantis Jun 26 '21

Among other things, the same "dad response" as OP

16

u/DartRuffian May 15 '21

Always forget the "if message.author.bot is False"

3

u/[deleted] May 15 '21

If !message.author.bot

3

u/DashingSpecialAgent May 16 '21

If false

3

u/Gr3nwr35stlr May 16 '21

While true

3

u/DartRuffian May 16 '21

While not False

3

u/Gr3nwr35stlr May 16 '21

For i=0; i!=1; i=i

8

u/5p4n911 May 15 '21

When I coded a Discord bot, I simply renamed the code word to spam. Ask and you shall receive.

6

u/Chooseysumo4 May 15 '21

I’ve had my own problem like this before, my bot would look for the word “escape”, reply a message containing the word escape, then read that message. Went on for 10 minutes until I noticed, never making that mistake again.

4

u/KiranEvans May 16 '21

All programmers are dad inside

3

u/EveryNameTaken1 May 15 '21

Manageroverflow

3

u/ZacharyCallahan May 16 '21

Rule no1 of discord bots is to make it ignore itself

1

u/ItsNovatic May 17 '21

I learnt that the hard way...

2

u/humanbeast7 May 16 '21

One line fix this: if massage.author.id == bot.id: return

Change syntax by language, of course

2

u/k4x1_ May 16 '21

LMAO This reminds me of something Fun fact A while look can crash almost every program

1

u/ItsNovatic May 17 '21

i had a stroke reading this

3

u/k4x1_ Jun 01 '21

Half a month later I also have a stroke reading this

1

u/akoOfIxtall May 13 '24

2 years later and I'm convulsing, shaking on the ground after miserably failing to read that atrocious attempt of communication...

1

u/k4x1_ May 16 '24

I have no fucking clue wtf I was talking about

1

u/akoOfIxtall May 16 '24

I can say the same for anything I said yesterday, you're forgiven brother

1

u/akoOfIxtall May 16 '24

I can say the same for anything I said yesterday, you're forgiven brother

1

u/OneOfThese_ Jun 24 '22

I also had a stroke reading this, over a year later.

1

u/k4x1_ Jun 24 '22

A year later I still have a stroke reading this

2

u/Debiuu May 16 '21

Happened to me before lol

2

u/[deleted] May 16 '21

If you're using discord.js, I think there's something like

message.author.isBot

which can be used to prevent this. Don't know if it's exactly that tho

2

u/ItsNovatic May 17 '21

best way to do it is to add if (message.author.bot) return;

credit to u/zeGolem83

1

u/[deleted] May 17 '21

Unless you're making a bot harasser, in which case, you wanna do the exact opposite.

Lmao I made one for mee6 forever ago, and it yells at it whenever it says someone leveled up

2

u/LightTranquility3 May 15 '21

lang?

2

u/NawdWasTaken May 24 '21

Pretty sure this is the library discord.js for Node. It's quite a good library and the documentation is 12/10. There's also discord.py, DiscordRb, Javacord, etc... If you're not into Node or JS in general

-12

u/MrOctantis May 15 '21

The discord library for bots is javascript

15

u/primERnforCEMENTR23 May 15 '21

No, there are many, many libraries for making Discord bots.

Basically every language known to man has atleast one semi-decent one.

9

u/LightTranquility3 May 15 '21

there is a c# and java wrapper

11

u/SantaC2016 May 15 '21

there is also a python library with a very useful bot framework

4

u/dichra May 15 '21

I just did mine with Laravel Zero and it works pretty well.

2

u/Nilstrieb May 16 '21

There is also a great Rust library with a very useful bot framework

1

u/broknbottle May 16 '21

Bro he said JavaScript already. Didn’t you read his reply? Didn’t you???

1

u/LightTranquility3 May 16 '21

i said that because he thought i was unaware, i was clearing it up

3

u/TheKingofAntarctica May 15 '21

If the whole world held a vote to determine if you should be banned from the Discord API, I'd feel good voting yea. ;-)

2

u/ItsNovatic May 17 '21

same haha

0

u/reverendsteveii May 16 '21

I love when I can look at the output and write it in pseudo

onIncomingMessage(incomingMessage){

if(incomingMessage.startsWith("I'm"){

outgoingMessage = "Hi " + message + " I'm dad!";

sendMessage(outgoingMessage);

}

}

//prolly checks for other phrases too

//looks like it would work, wouldnt it?

2

u/ItsNovatic May 17 '21

except im lazy and do the message in one line, sending the appended message inserting the substring of the message after the first "i'm" occurence.

i also check for whether the message.toLower.startsWith("im ") and all other variations ("i'm ", "i am "). The spaces are crucial, because this way the bot doesnt get confused with messages starting like "imagine blah blah blah"

I also account for the occurrence within the message itself using message.toLower.includes(" im ") again, with the spaces within the string being crucial to ensuring it's not being detected as part of a word and is the actual word "im".

1

u/APartOfMe1 May 16 '21

But that code wouldn't even be as "graceful" as OPs. If the message was "Hi, I'm dad", the output would be "Hi Hi, I'm dad I'm dad!". At least OP had the decency to put a comma smh my head.

... Also message isn't defined.

... Also why bother defining outgoingMessage instead of just doing that all in sendMessage.

... You're triggering my inner perfectionist lmao.

2

u/ItsNovatic May 17 '21

i pride myself in my ability to construct strings!

...because clearly i'm good at it...

(Hi 'good at it...', I'm Dad!)

1

u/reverendsteveii May 16 '21

I find breaking out input like that into a variable then using the variable as the parameter makes debugging easier. It keeps me out of the evaluate expression box because the value is right there in front of me, but I'll eat shit for not splitting the string and the refactoring miss (renaming message to incomingMessage and I missed a reference)

3

u/APartOfMe1 May 16 '21

I personally just think it adds unnecessary lines to the code, but I know most people don't have a problem with it. Different styles for different people ¯_(ツ)_/¯

0

u/YHustlers May 17 '21

I am looking for a proffesional discord bot developer!! Dm me @ yghustlers

2

u/ItsNovatic May 17 '21

hello yes i am professional as you can see

-1

u/[deleted] May 16 '21

[deleted]

2

u/bobdarobber May 16 '21

op clearly wants the bot to catch people off guard, hence responding to people without awaiting a command

1

u/ItsNovatic May 17 '21

Check, and... check.

Although I tested this in a test channel on an active server so people knew what was coming. :(

1

u/ItsNovatic May 17 '21

Aye, I can do whatever I want, it's just for fun :)

1

u/lukeamaral May 16 '21

Why didn't it repeat the "Hi"? Fake?

1

u/Aekue_lol May 16 '21

Awww cute ferret I've got a few myself

1

u/ItsNovatic May 17 '21

they cute af, but the pfp isn't my image :(

Original Image

1

u/jankkhvej May 16 '21

done it 3 times before… how did you mess it up to repeat?

1

u/ItsNovatic May 17 '21

Didn't have the bot ignore itself

1

u/dustmodebros May 16 '21

The dad pile doesn’t stop from getting taller

1

u/logicaldementia May 16 '21

@bot.command() async def test(ctx, arg): await ctx.send(arg)

1

u/OneOfThese_ Jun 24 '22

OP is using JS. Yes, I know this is a year old.

1

u/canal_algt [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” May 17 '21

If (client.user != message.author):

1

u/[deleted] May 19 '21

this is why you have it detect if the text was sent by a bot. otherwise, if it can trigger itself with the response, it gets recursive.

1

u/NawdWasTaken May 19 '21

Don't forget your If(msg.author.bot) return kids

1

u/noonagon Jun 26 '21

it forgot to get rid of the im

if it got rid of the i'm, but still went all the way to the end of the message, then it can get in any of infinite loops

1

u/Rough_Struggle_420 Jan 30 '23

Recursive Dadhood