-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNavCoffeeBot.py
76 lines (59 loc) · 2.24 KB
/
NavCoffeeBot.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
import discord
import asyncio
import time
import requests
import threading
import os
from discord.ext.commands import bot
from discord.ext import commands
Client = discord.Client()
client = commands.Bot(command_prefix = "?")
token = "ENTER YOUR BOT TOKEN HERE"
channel_id = "ENTER THE CHANNEL ID THE BOT SHOULD OPERATE IN HERE"
# Reports when bot is ready
@client.event
async def on_ready():
print('NavCoffeeBot Ready')
print('Username: ' + str(client.user.name))
print('Client ID: ' + str(client.user.id))
print('Invite URL: ' + 'https://discordapp.com/api/oauth2/authorize?client_id=' + client.user.id + '&permissions=8&scope=bot\n')
# When bot is ready start reading wallet data
update_wallet()
# Message Listens / Commands
@client.event
async def on_message(message):
# Give available coffee(s) on !coffee
if message.content.upper() == "?COFFEE":
coffee = get_coffees()
redeems = get_redemptions()
coffees_redeemable = str(coffee-redeems)
reply = "There are " + coffees_redeemable + " coffees available for redemption! :coffee:"
await client.send_message(client.get_channel(channel_id), reply)
# Read coffees donated
def get_coffees():
coffee_file = open("/home/pi/Documents/coffees_donated2.txt", "r")
return int(coffee_file.readline())
# Read redemptions
def get_redemptions():
redeem_file = open("/home/pi/Documents/redemptions2.txt", "r")
return int(redeem_file.readline())
# Read up to date wallet data, compare to known wallet data
def update_wallet():
threading.Timer(5.0, update_wallet).start()
dono_file = open("/home/pi/Documents/donations2.txt", "r")
donos = int(dono_file.readline())
dono_file.close()
bot_file = open("/home/pi/Documents/bot.txt", "r")
bot_donos = int(bot_file.readline())
bot_file.close()
if donos > bot_donos:
client.loop.create_task(send_update())
bot_file_tmp = open("bot.txt.tmp", "w")
bot_file_tmp.write(str(donos))
bot_file_tmp.close()
os.rename('bot.txt.tmp', 'bot.txt')
async def send_update():
await client.wait_until_ready()
await client.send_message(client.get_channel(channel_id), "Coffee Donated! :coffee:")
# Run the bot
client.run(token)