-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
110 lines (89 loc) · 3.72 KB
/
Copy pathmain.py
File metadata and controls
110 lines (89 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import discord
# Make sure we're using the correct version of Discord.py
if discord.__author__ != "Rapptz":
print("Error: Please use the rewrite version of Discord.py.")
print("Check the README for instructions.")
exit()
import requests, asyncio, platform
from discord.ext.commands import Bot
# End imports
bot = Bot(command_prefix='!', case_insensitive=True, description='A bot for comparing cryptocurrency values.')
coin_data = requests.get("https://www.cryptocompare.com/api/data/coinlist/").json()['Data']
# User typing is checked against this list of valid symbols
coin_list = list(coin_data.keys())
coin_list.append("USD")
invalid_coin = "Error: {} is not a valid symbol. Please check your typing."
# Place the token in a file called "token.txt"
# IMPORTANT: Make sure there is no newline at the end!
# You will get cryptic errors.
# Try "tr -d '\n' < token.txt" (*NIX only)
TOKEN = open("token.txt", "r").read()
# So we can have our neat little custom help function
bot.remove_command("help")
# Help strings; sent when a user types "!help {command_here}"
help_strings = {
"price": "\nExample: Getting the price of a Bitcoin in USD:\n` !price USD BTC`",
"image": "\nExample: Getting the Bitcoin logo:\n` !image BTC`",
"name": "\nExample: Finding out what \"ETC\" means:\n` !name ETC`",
"algo": "\nExample: "
}
# Commands / events below
@bot.event
async def on_ready():
print(
"Logged in\n"
+ str(bot.user.name) + "\n"
+ str(bot.user.id)
)
# NOTE: The whole point of the help strings is so that this command is
# short and readable. Do not change.
@bot.command()
async def help(ctx, cmd=None):
# Print a list of commands if one is not supplied
if cmd is None:
await ctx.send("List of commands:\n")
for i in help_strings.keys():
await ctx.send("`!" + i + "`")
# Make sure the command typed actually exists
if cmd in help_strings.keys():
await ctx.send(help_strings[cmd])
else:
# Get rid of annoying errors when no command is passed
if cmd is not None:
await ctx.send(cmd + ": Command not found")
# TODO: Clean this up at some point, it's a mess.
@bot.command()
async def price(ctx, to_sym, from_sym):
if to_sym in coin_list and from_sym in coin_list:
res = requests.get("https://min-api.cryptocompare.com/data/pricemulti?fsyms="+from_sym+"&tsyms="+to_sym)
await ctx.send("Price of 1 " + from_sym + ": " + str(res.json()[from_sym][to_sym]) + " " + to_sym)
await ctx.send("Data from <https://www.cryptocompare.com>")
else:
if to_sym not in coin_list:
await ctx.send("Error: " + to_sym + " is not a valid symbol. Please check your typing.")
if from_sym not in coin_list:
await ctx.send("Error: " + from_sym + " is not a valid symbol. Please check your typing.")
await ctx.send("Please note that USD is the only non-cryptocurrency accepted.")
@bot.command()
async def image(ctx, coin):
if coin in coin_list:
await ctx.send("https://www.cryptocompare.com" + coin_data[coin]["ImageUrl"])
else:
await ctx.send(invalid_coin.format(coin))
@bot.command()
async def name(ctx, coin):
if coin in coin_list and coin is not "USD":
await ctx.send(coin_data[coin]["FullName"])
else:
await ctx.send(invalid_coin.format(coin))
@bot.command()
async def algo(ctx, coin):
if coin in coin_list and coin is not "USD":
await ctx.send(coin_data[coin]["Algorithm"])
else:
await ctx.send(invalid_coin.format(coin))
@bot.command()
async def dedede(ctx):
await ctx.send("https://cdn.discordapp.com/attachments/477252163899228160/477273917795467284/image.jpg")
# End commands / events
bot.run(TOKEN)