![]() |
|
Discord.py - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Python (https://sinister.li/Forum-Python) +--- Thread: Discord.py (/Thread-Discord-py) |
Discord.py - Blink - 02-14-2017 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.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')RE: Discord.py - Inori - 02-15-2017 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. RE: Discord.py - m0dem - 02-15-2017 Good job @'Ender', remind me to +rep you tomorrow. (I've exceeded my daily allowance )I really need to learn asyncio. You seem like a pretty good programmer. Have a Github? RE: Discord.py - Inori - 02-15-2017 (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. RE: Discord.py - m0dem - 02-15-2017 (02-15-2017, 12:43 AM)Inori Wrote:(02-15-2017, 12:38 AM)m0dem Wrote: I really need to learn asyncio. I do agree. This twisted my brain in a knot: https://snarky.ca/how-the-heck-does-async-await-work-in-python-3-5/ RE: Discord.py - Blink - 02-15-2017 (02-15-2017, 12:38 AM)m0dem Wrote: Good job @'Ender', remind me to +rep you tomorrow. (I've exceeded my daily allowance 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. RE: Discord.py - Mr.Kurd - 02-15-2017 Thank you man, I wanna make one with Java but I haven't any time .Good Work RE: Discord.py - DarkMuse - 02-15-2017 Looks great! Thanks for the script friend! |