chevron_left chevron_right
Login Register invert_colors photo_library


Stay updated and chat with others! - Join the Discord!
Thread Rating:
  • 0 Vote(s) - 0 Average


Discord.py filter_list
Author
Message
Discord.py #1
Thanks to @m0dem for the idea to write this.

Today I will be showing you an unofficial API for Discord called "Discord.py"

I will be showing you how to write a simple bot, for more detailed information, please go to https://discordpy.readthedocs.io/en/latest/

Note: Discord.py was made for python3, not 2.


Let,s begin!
First install the package:
Code:
pip3 install discord.py

Then, you'll want to create a new python script for your bot.
You'll want to write these lines at the top of the file:
Code:
import discord
import asyncio

Next:
Code:
client = discord.Client()
@client.event
async def on_ready():
   print('Logged in as')
   print(client.user.name)
   print(client.user.id)
   print('------')
   await client.change_presence(game=discord.Game(name='with Ender'))
@client.event
async def on_message(message):
#DEBUG MODE:
#    print("CHANNEL "+message.channel)
#    print("CONTENT "+message.content)
#    print("AUTHOR "+message.author)
#    print("SERVER "+message.server)
   if message.content == '~help':
       await client.send_message(message.channel, ':small_blue_diamond: My Commands:')
       await client.send_message(message.channel, """```
[~help : Shows this screen]
[~sleep : sleep for 5 seconds]
[~hi: say hi!]`""")
   elif message.content == '~sleep':
       await asyncio.sleep(5)
       await client.send_message(message.channel, 'Done sleeping')
   
   elif message.content == '~hi':
       await client.send_message(message.channel, 'Hello!')

You can pretty much see the flow here, the playing tag is Playing with Ender, you can change that if you want, it's the line before the second @client.event

To add more commands just add another elif statement in this form:
Code:
elif message.content == "Whatever command you want":
   await client.send_message(message.channel, "What to send back!")
Pretty simple right?

Stuff to run once goes in the first @client.event, stuff to run on a condition goes in the second.

Uncomment the debug mode stuff to see what channel/server/user the message is from.

Don't forget, you have to add this to the last line of the script:
Code:
client.run('Your Bot Token')
(This post was last modified: 02-14-2017, 11:44 PM by Blink.)


(11-02-2018, 02:51 AM)Skullmeat Wrote: Ok, there no real practical reason for doing this, but that's never stopped me.

[+] 2 users Like Blink's post
Reply

RE: Discord.py #2
Nice and to the point, I like it. You might want to look into the argparse module for your prompt; it takes a lot of the maintenance out of writing help messages and such, plus all the results get returned in their own namespace, which is super convenient.

[+] 1 user Likes Inori's post
Reply

RE: Discord.py #3
Good job @Ender, remind me to +rep you tomorrow. (I've exceeded my daily allowance xD)
I really need to learn asyncio.
You seem like a pretty good programmer. Have a Github?
(This post was last modified: 02-15-2017, 12:38 AM by m0dem.)

Reply

RE: Discord.py #4
(02-15-2017, 12:38 AM)m0dem Wrote: I really need to learn asyncio.

asyncio and decorators are so out of place in Python. They're irreplaceable in terms of utility, but it's really confusing to get used to that part of the syntax.

Reply

RE: Discord.py #5
(02-15-2017, 12:43 AM)Inori Wrote:
(02-15-2017, 12:38 AM)m0dem Wrote: I really need to learn asyncio.

asyncio and decorators are so out of place in Python. They're irreplaceable in terms of utility, but it's really confusing to get used to that part of the syntax.

I do agree.
This twisted my brain in a knot: https://snarky.ca/how-the-heck-does-asyn...ython-3-5/

Reply

RE: Discord.py #6
(02-15-2017, 12:38 AM)m0dem Wrote: Good job @Ender, remind me to +rep you tomorrow.  (I've exceeded my daily allowance  xD)
I really need to learn asyncio.
You seem like a pretty good programmer.  Have a Github?

I have an older Github, but I don't use it much and prefer not to give it out.
I'll make a new one soon.


(11-02-2018, 02:51 AM)Skullmeat Wrote: Ok, there no real practical reason for doing this, but that's never stopped me.

Reply

RE: Discord.py #7
Thank you man, I wanna make one with Java but I haven't any time Sad .
Good Work

Reply

RE: Discord.py #8
Looks great! Thanks for the script friend!
Scientia potentia est

[Image: inkexplosion.jpg]

Reply






Users browsing this thread: 1 Guest(s)