Last active
March 16, 2019 10:35
-
-
Save Tetraquark/911eb07052be10f0bb1323adb4056f86 to your computer and use it in GitHub Desktop.
Example of simple discord chatbot based on discord.py library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Example of simple discord chatbot based on discord.py library | |
# For article (Russian language): http://tetraquark.ru/archives/377 | |
# | |
import discord | |
import asyncio | |
import requests | |
DISCORD_BOT_TOKEN = 'DISCORD_APP_TOKEN' | |
BTC_PRICE_URL_coinmarketcap = 'https://api.coinmarketcap.com/v1/ticker/bitcoin/?convert=RUB' | |
client = discord.Client() | |
@client.event | |
async def on_ready(): | |
print('Logged in as') | |
print(client.user.name) | |
print(client.user.id) | |
print('------') | |
@client.event | |
async def on_message(message): | |
if message.content.startswith('!btcprice'): | |
print('[command]: btcprice ') | |
btc_price_usd, btc_price_rub = get_btc_price() | |
await client.send_message(message.channel, 'USD: ' + str(btc_price_usd) + ' | RUB: ' + str(btc_price_rub)) | |
def get_btc_price(): | |
r = requests.get(BTC_PRICE_URL_coinmarketcap) | |
response_json = r.json() | |
usd_price = response_json[0]['price_usd'] | |
rub_rpice = response_json[0]['price_rub'] | |
return usd_price, rub_rpice | |
client.run(DISCORD_BOT_TOKEN) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment