-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
183 lines (161 loc) · 6.16 KB
/
bot.py
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import os, sys, traceback, time, requests # standard library
import discord
from discord.ext import commands # basic bot imports
import format # custom libraries
import foldingathome as fah
from environs import Env
import asyncio
env = Env()
env.read_env()
team_number = 235150
channelA = env.int("CHANNEL_A")
channelB = env.int("CHANNEL_B")
channelC = env.int("CHANNEL_C")
channelD = env.int("CHANNEL_D")
embedcolor = 0x4286F4
prefix = env.str("PREFIX")
token = env.str("TOKEN")
bot = commands.Bot(command_prefix=prefix)
bot.remove_command("help")
@bot.command(pass_context=True)
async def restart(ctx):
if ctx.author.id == 330404011197071360:
await ctx.send(f"♻ {ctx.author.mention} just restarted me.")
print('restart')
@bot.command(pass_context=True)
async def update(ctx):
if ctx.author.id == 330404011197071360:
await ctx.send(f"♻+ {ctx.author.mention} just restarted me and asked for an update.")
print('update')
@bot.command(pass_context=True)
async def ping(ctx):
try:
r = requests.get(
"https://discordapp.com/api", headers={"Authorization": f"Bot:{token}"}
)
latency = r.elapsed.total_seconds()
embed = discord.Embed(
title="Ping", description=f"Ping data for {bot.user.name}", color=embedcolor
)
embed.add_field(name=":ping_pong:", value=f"{latency*1000}ms", inline=False)
await ctx.channel.send(embed=embed)
except:
pass
@bot.command(pass_context=True)
async def help(ctx):
embed = discord.Embed(
title="__Help__",
description="Here's a list of commands that you can use.",
color=embedcolor,
)
embed.add_field(
name="__stats__",
value="Usage:\n`fold stats`\nReturns stats on your servers team\n`fold stats <donor>`\nReturns info on a user in your team",
inline=False,
)
embed.add_field(
name="__team__",
value="Usage:\n`fold team <team number>`\nReturns stats on the given team.",
inline=False,
)
await ctx.channel.send(embed=embed)
@bot.command(pass_context=True)
async def team(ctx, team=team_number):
team = fah.Team(team)
description = f"Team {team.name}"
rank = f"Rank out of {team.total_teams}"
embed = discord.Embed(
title="Folding@Home statistics", description=description, color=embedcolor
)
embed.add_field(name="Total credits", value=str(team.score), inline=False)
embed.add_field(name="Total work units", value=str(team.work_units), inline=False)
embed.add_field(name=rank, value=str(team.rank), inline=False)
if team.total_donors == 1000:
embed.add_field(name="Total number of donors", value="1000+", inline=False)
else:
embed.add_field(
name="Total number of donors", value=str(team.total_donors), inline=False
)
print(team.logo)
embed.set_thumbnail(url=team.logo)
await ctx.channel.send(embed=embed)
@bot.command(pass_context=True)
async def stats(ctx, donor=None):
if donor:
try:
donor = fah.Donor(donor, team_number)
except:
await ctx.channel.send(
"Something went wrong. You may have entered an invalid donor or there may be a problem reaching Folding@Home, please try again.\nIf this has happened before, please try again later."
)
title = "Folding@Home statistics for '{}'".format(donor.name)
description = "Donor '{}'".format(donor.name)
embed = discord.Embed(title=title, description=description, color=embedcolor)
embed.add_field(name="Total credits for team", value=donor.score, inline=False)
embed.add_field(
name="Total work units completed for team",
value=donor.work_units,
inline=False,
)
embed.add_field(name="Rank", value=donor.rank, inline=False)
embed.set_thumbnail(url=donor.team["logo"])
await ctx.channel.send(embed=embed)
else:
team = fah.Team(team_number)
description = "Team {}".format(team.name)
rank = "Rank out of {}".format(team.total_teams)
embed = discord.Embed(
title="Folding@Home statistics", description=description, color=embedcolor
)
embed.add_field(name="Total credits", value=str(team.score), inline=False)
embed.add_field(
name="Total work units", value=str(team.work_units), inline=False
)
embed.add_field(name=rank, value=str(team.rank), inline=False)
if team.total_donors == 1000:
embed.add_field(name="Total number of donors", value="1000+", inline=False)
else:
embed.add_field(
name="Total number of donors",
value=str(team.total_donors),
inline=False,
)
embed.set_thumbnail(url=team.logo)
await ctx.channel.send(embed=embed)
async def get_fah_stats():
team = fah.Team(235150)
highest_scorer = team.highest_scorer
most_wus = team.most_wus
score = team.score
work_units = team.work_units
highest_scorer["credit"] = format.convert_int(highest_scorer["credit"])
most_wus["wus"] = format.convert_int(most_wus["wus"])
return highest_scorer, most_wus, score, work_units
async def update_count():
while True:
print("update")
stats = await get_fah_stats()
hs, mw, ts, twus = stats
await bot.get_channel(channelA).edit(
name=await format.convert_string(hs["name"] + " : ") + hs["credit"]
)
await bot.get_channel(channelB).edit(
name=await format.convert_string("total score" + " : " + str(ts))
)
await bot.get_channel(channelC).edit(
name=await format.convert_string("total wus" + " : " + str(twus))
)
await bot.get_channel(channelD).edit(
name=await format.convert_string(mw["name"] + " : " + mw["wus"] + " wus")
)
await asyncio.sleep(600)
@bot.event
async def on_ready():
print("Logged in as")
print(bot.user.name)
print(bot.user.id)
print("------")
sys.stdout.flush()
await bot.change_presence(activity=discord.Game(name=f"{prefix}help"))
await update_count()
bot.run(token)