Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to Make A Discord Bot - Part 5 (Updated 2021)
FishballNooodle (183)

Hi everyone, I couldn't think of an intro so here you go. Firstly, I need to apologize for sending this thing so late. I was actually working on a python program that could read online Google Spreadsheets, anyways. So today I will be teaching you how to get actual user input, not just the command portion.
So what we will be working on today is a Suggest Function, so this is how it will work.

It will also print to a specific channel for suggestions like this one:

Ooooh!!! Do you see some reactions over there? Yes, we will be learning how to add reactions.
Before that, let's learn how to get the words they say after |suggest.
Wait I thought you could just add a variable there like you did last time.
Actually is not as easy as that anymore. The input we got last time was a number, this time we want everything after that. The difference is there may be spaces like the example above and the bot might take the first element before the space only. What we have to do to avoid that is to add a separator(delimiter), like this:
@client.command()
async def suggest(ctx,*,suggestion):
In this case, we have to use an * as an independent argument before the suggestion variable we used to store everything typed out after that. In the example picture just now "Test 04" would have been stored as a variable.

Note that this is called a keyword argument and you can only have 1 of these for each command only, preferably the last argument.

That was the main part done, now we have to turn it into an embed. I won't be explaining much of this part because I did that in Part 4.

@client.command()
async def suggest(ctx,*,suggestion):
  author=ctx.message.author
  file=open("suggestions.txt","a+")
  file.write(str(author)+" : "+suggestion+"\n")
  embed = discord.Embed(
    title = 'Suggestion',
    description = "This Was Suggested By",
    colour = discord.Colour.orange()
  )

  embed.set_footer(text = "Made By @Fishball_Noodles with .py")
  embed.set_author(name = "Moderation#2520")
  embed.add_field(name = author, value = suggestion)
  await ctx.send("Suggestion Submitted")
  channel = client.get_channel(123456789123456789)
  msg = await channel.send(embed=embed)
  await msg.add_reaction('👍')
  await msg.add_reaction('👎')

This was taken straight from my code and I will break down what I did.
So I actually used ctx again to get the author object, in this case since I sent it, the author would be me.
Next, as you can see I actually wrote the suggestion to a text file, this can be an alternative to keep track of the suggestions instead of going to Discord itself. I will be just explaining how it works briefly since this is a Discord Tutorial.

So I opened a text file called suggestions in append mode and printed a new line for every suggestion together with the author and suggestion as seen over here.
file.write(str(author)+" : "+suggestion+"\n")

Sending to specific Channel

Now for the sending part. Over here I didn't use ctx.send(embed=embed), because I wanted to send to the specific suggestions channel.
To do that, we use the get_channel function, where it will return the appropriate channel object when given the correct Channel ID.

To find your channel ID, read my Developer Mode Tutorial Here

Now we have the channel object, we can use the .send method of the channel class to send a message to that specific channel.

Note that this I completely optional. If you are okay with it being sent in the same channel, feel free to use ctx.send()

Adding Reactions

Wait why did you do msg = channel.send(embed=embed)
That leads us to the next topic of adding reactions. To use the .add_reactions method, we need the message class. To save us the hassle of finding the message we sent, we can assign that action of creating a message to the msg variable.
The add_reactions method takes in 1 argument which is the emoji object itself. To get the emoji, we can copy and paste the emoji like above.

Note that this is not advisable because some emojis may not be able to copy and paste

If your's is unable to copy and paste we will use client.get_emoji(id), where ID is the ID of your emoji(not alias). I'll cover more of it in another tutorial.
emoji = client.get_emoji(12345678912)
msg.add_reactions(emoji)

Lastly, add your
client.run(token)
to run your bot

And that is the end of this tutorial, if you do want to see more of these, hit the upvote button, really appreciate it.
Also, you can support me by sending me a message in the comments section below saying that you finished the tutorial.
Lastly, if you faced any problems during the process, feel free to ask me in the comments section below.
see you guy's next time! :)

Comments
hotnewtop
FishballNooodle (183)

Upvote this comment if you think i should make a Series on handling Excel files with Python

CodingRobot12 (183)

These tutorials are so good!

FishballNooodle (183)

@CodingRobot12
Thanks,all your support helps me make more
:)

FishballNooodle (183)

@CodingRobot12
Also Do look for my Part - 6 That I will be posting in about 6 Hours from now

FNAFar1 (0)

i want to do command like (add_reaction 👍 magID)

HahaYes (1861)

sigh I really regret inventing cycle squeezing

polarbear2015mw (0)

on part 6 can you teach us how to get rid of roles and assign roles using an command & join messages

FishballNooodle (183)

@polarbear2015mw
Ooh, yes I will be going through roles next tutorial.
I didn't plan on doing join messages but I will do it:)

JenniferPoon (0)

What' s inside that suggestions text file?

FishballNooodle (183)

@JenniferPoon
Oh it's just the name of the user and the suggestion they made.
It's optional, I just thought it would be cool

JenniferPoon (0)

But if i have that file, do i just leave it blank so next time when someone suggests it, it will be shown there? @FishballNooodle

JenniferPoon (0)

@FishballNooodle Oh sorry I have another question. Im not sure what i have to put in for the ID in guild_id and guild_channel. Could you please explain?

FishballNooodle (183)

@JenniferPoon
can you be slightly more clear on what you are trying to do

JenniferPoon (0)

@FishballNooodle Im just using the code you provided for suggestion but i see there are lots of ID nunmber so im not sure what they refer to.

FishballNooodle (183)

@JenniferPoon
Ok so in my code I was trying to send the embed to the same channel all the time no matter where it was being sent from.
So we do channel.send(embed=embed)
To get channel, U need channel ID, to get channel ID right click the channel in your server and click copy Id. Then do this, channel=client.get_channel(Put ID Here)

JenniferPoon (0)

@FishballNooodle
Im still confused about the ID as to where they should go and Im getting the issue of the channel not being registered. For guild_id, what should i put there?

FishballNooodle (183)

@JenniferPoon
I didn't even use guild what are you even talking about.
Right now you are definitely not following my tutorial, but that's ok, you just need to tell me what you are trying to do, I can help

JenniferPoon (0)

@FishballNooodle
I did followed your tutorial and here's my code if you don't believe me.

@client.command()
async def suggest(ctx,*,suggestion):
author = ctx.message.author
file = open("suggestions.txt", "a+")
file.write(str(author) + " : " + suggestion+ "\n")
embed = discord.Embed(title = 'Suggestion', description = "This was suggested by", colour= discord.Colour(0xaa4fd5))
embed.set_footer(text = "Made by @Hermione Granger with .py")
embed.set_author(name = "Fawkes#9237")
embed.add_field(name = author, value = suggestion, inline = False)
if ctx.message.guild.id == 705707141633409034: #hogwarts Student ID
print("suggest in '{}'".format(ctx.message.guild))
channel = client.get_channel(705101243185496084) #suggestion channel ID
elif ctx.message.guild.id == 705442554455523329: #order of the phoenix ID
print("Suggested in {}".format(ctx.message.guild))
channel = client.get_channel(705101243185496084) #suggestion channel ID
else:
await ctx.send("channel not registered")

await ctx.send("suggestion submitted")
ctx.msg = await channel.send(embed=embed)
await ctx.message.add_reaction('👍')
await ctx.message.add_reaction('👎')

So Im referring to these two lines specifically:
if ctx.message.guild.id == 705707141633409034:
print("suggest in '{}'".format(ctx.message.guild))
You might have already noticed the guild.id. That's what it confuses me. Im not sure if i should put the member ID there or what.

FishballNooodle (183)

@JenniferPoon
Bruh, you didn't follow my code.
You don't just copy like that with no understanding. If you can explain how that works I'm fine but you don't. FOLLOW MY TUTORIAL, else don't ask what is what. I'm doing it in a series,if I could just let all of you copy then why did I make this tutorial, I could have put in under share.
Please Guys.

polarbear2015mw (0)

Repl has an autotimeout thing and I want to make sure my bot will be up 24/7 how is that possible?

FishballNooodle (183)

@polarbear2015mw
That's exactly what repl wants you to do, which is to pay them.
Alternatively, you can run it on IDLE or Atom, but do remember to do pip install discord.py in cmd

HarshVardhan19 (16)

@polarbear2015mw replit kills the running projects after 5 min. You can use a flask app and put up a monitor on uptimerobot to ping it every 5 min. This way, it rarely goes down. My bots stay online for days. But yes if you're hosting a small bot for a small number of servers, you can use this way, else i would suggest buy a vps.

You can try reading this for some reference on how to use the flask app to keep the bot online 24/7

AstOwOlfo (258)

get the student dev pack from github, it’ll give you free hacker plan. after that you can enable an “always online” repl @polarbear2015mw

NoelB33 (347)

Just saying, a lot of suggestions will be lost, because files don’t persist the way you think they do on replit. Maybe have it send a message to you in DM’s every time a suggestion is made?

FishballNooodle (183)

@NoelB33
What do you mean.
In my other projects, thr files work well

NoelB33 (347)

You will loose data with the method you use. If you make a change to a file on replit with the program, it will not persist. @FishballNooodle

FishballNooodle (183)

@NoelB33
I'm using append mode it won't truncate the file

NoelB33 (347)

@FishballNooodle
Whatever you append won’t save right, and you will see a lot of data loss. That’s how replit works.

NoelB33 (347)

It doesn’t. Multiple replit staff have said it on the discord. @FishballNooodle

FishballNooodle (183)

@NoelB33
OK noted, hopefully that won't happen to me

NoelB33 (347)

It will, but you’ll never notice because you don’t known when people make suggestions @FishballNooodle

DamariLovelace (0)

Do you suggest doing it on phone or computer

FishballNooodle (183)

@DamariLovelace
Phone might be too slow cos it needs to import a big module

JustAWalrus (1190)

Haha, you only remained top of the tutorials section for 5 minutes.

JustAWalrus (1190)

At the top of trending for 5 minutes. @FishballNooodle

FishballNooodle (183)

@Wuru
oh...urs took over
but it was meaningful