How to Make A Discord Bot - Part 5
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, lets 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 maybe 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:
async def suggest(ctx,*,suggestion):
In this case we have to use an * as a 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 newline 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()
Wait why did you do msg = channel.send(embed=embed)
That leads us into 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 emoji's 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)
Lastly, add your
to run your bot
And that is the end of this tutorials, 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, fell free to ask me in the comments section below.
see you guy's Next time! :)