Discord.py 02-14-2017, 11:38 PM
#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:
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:
Next:
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:
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:
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.pyThen, 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 asyncioNext:
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!")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.)











![[+]](https://sinister.li/images/modern/collapse_collapsed.png)









)


.![[Image: inkexplosion.jpg]](http://i0.wp.com/techverse.net/wp-content/uploads/2013/09/inkexplosion.jpg)