Skip to content

Commit 3eb6ff0

Browse files
authored
Merge pull request #12 from Hjorthdx/Development-branch-v2
Development branch v2
2 parents b368ed7 + e30eab9 commit 3eb6ff0

11 files changed

Lines changed: 151 additions & 37 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,4 @@ dmypy.json
129129

130130
# Pyre type checker
131131
.pyre/
132+
*.pyc

Constants.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# CONSTANTS
2+
DEFAULT_WORKTIME = 50
3+
DEFAULT_BREAKTIME = 10
4+
DEFAULT_PLAYER_PATH = "C:/Program Files (x86)/ffmpeg/bin/ffmpeg.exe"
5+
SECONDS_IN_HOUR = 3600
6+
SECONDS_IN_MINUTE = 60

Player.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import discord, Constants
2+
3+
async def play(message):
4+
if "latex" in message.content:
5+
channel = message.author.voice.channel
6+
vc = await channel.connect()
7+
vc.play(discord.FFmpegPCMAudio(executable=Constants.DEFAULT_PLAYER_PATH, source="C:/Users/Sren/Documents/GitHub/DiscordKarmaBot/mp3-files/LatexBusters.mp3"))
8+
while vc.is_connected():
9+
if not vc.is_playing():
10+
await vc.disconnect()
11+
elif "bamse" in message.content:
12+
channel = message.author.voice.channel
13+
vc = await channel.connect()
14+
vc.play(discord.FFmpegPCMAudio(executable=Constants.DEFAULT_PLAYER_PATH, source="C:/Users/Sren/Documents/GitHub/DiscordKarmaBot/mp3-files/test.mp3"))
15+
while vc.is_connected():
16+
if not vc.is_playing():
17+
await vc.disconnect()
18+
elif "inspiration" in message.content:
19+
channel = message.author.voice.channel
20+
vc = await channel.connect()
21+
vc.play(discord.FFmpegPCMAudio(executable=Constants.DEFAULT_PLAYER_PATH, source="C:/Users/Sren/Documents/GitHub/DiscordKarmaBot/mp3-files/Ole_Wedel.mp3"))
22+
while vc.is_connected():
23+
if not vc.is_playing():
24+
await vc.disconnect()
25+
26+
async def playTimerEnd(channel):
27+
vc = await channel.connect()
28+
vc.play(discord.FFmpegPCMAudio(executable=Constants.DEFAULT_PLAYER_PATH, source="C:/Users/Sren/Documents/GitHub/DiscordKarmaBot/mp3-files/test.mp3"))
29+
while vc.is_connected():
30+
if not vc.is_playing():
31+
await vc.disconnect()

Pomodoro.py

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,61 @@
1-
import time
2-
import datetime as dt
1+
import discord, asyncio, Constants, Player, datetime
32

4-
def startPomodoro(pomodoro_time, break_time):
5-
print("Pomodoro started")
6-
start_time = dt.datetime.now()
7-
pomodoro_time = pomodoro_time
8-
time_delta = dt.timedelta(0,pomodoro_time)
9-
pomodoro_end_time = start_time + time_delta
10-
break_time = break_time
3+
startingTime = 0
4+
workBool = False
115

12-
if start_time < pomodoro_end_time:
13-
print("Currently working")
6+
async def startTimers(message):
7+
global workLength
8+
global breakLength
9+
workLength, breakLength = getLengths(message.content)
10+
global startingTime
11+
startingTime = datetime.datetime.now()
12+
await workTimer(message, workLength, breakLength)
1413

14+
async def workTimer(message, workLength, breakLength):
15+
global workBool
16+
workBool = True
17+
await asyncio.sleep(workLength)
18+
await message.channel.send("WORKS OVER, STARTING BREAK")
19+
await Player.playTimerEnd(message.author.voice.channel)
20+
await breakTimer(message, breakLength)
1521

22+
async def breakTimer(message, breakLength):
23+
global workBool
24+
workBool = False
25+
await asyncio.sleep(breakLength)
26+
await message.channel.send("BREAKS OVER")
27+
await Player.playTimerEnd(message.author.voice.channel)
28+
29+
def getLengths(content):
30+
x = [int(s) for s in content.split() if s.isdigit()] # Gets all the digits from the string and saves in a list of integers.
31+
if len(x) == 0:
32+
return Constants.DEFAULT_WORKTIME * 60, Constants.DEFAULT_BREAKTIME * 60
33+
elif len(x) == 2:
34+
workLength = x[0]
35+
breakLength = x[1]
36+
return workLength * 60, breakLength * 60
37+
else:
38+
print('Wrong formatted user input')
39+
# Should maybe text this to the user
40+
41+
def calculateRemainingTime():
42+
global startingTime
43+
global workLength
44+
global workLength
45+
global workBool
46+
hours = 0
47+
duration = datetime.datetime.now() - startingTime
48+
durationInSeconds = duration.total_seconds()
49+
50+
if workBool:
51+
remainingTimeInSeconds = workLength - durationInSeconds
52+
else:
53+
remainingTimeInSeconds = breakLength - durationInSeconds
54+
55+
if remainingTimeInSeconds > Constants.SECONDS_IN_HOUR:
56+
hours, remainingTimeInSeconds = divmod(remainingTimeInSeconds, 3600)
57+
if remainingTimeInSeconds > Constants.SECONDS_IN_MINUTE:
58+
minutes, remainingTimeInSeconds = divmod(remainingTimeInSeconds, 60)
59+
60+
formattedRemainingTime=('%02d:%02d:%02d'%(hours,minutes,remainingTimeInSeconds))
61+
return formattedRemainingTime

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
# DiscordKarmaBot
2-
Discord bot that can keep track of user reactions and add karma to each person based on the emojis that other users react to their posts with
2+
Discord bot that can keep track of user reactions. User reactions will be stored on a database in form of karma to see who is the meme lord.
3+
4+
Incoming features:
5+
Pomodoro timer
6+
Bot joins and plays a mp3 file when the time is up
7+

TODOS.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# TODO
2+
# BOT PART
3+
(X) Bot skal forlade rummet selv efter mp3 fil er slut eller ved command måske.
4+
Change on_message !karma so it only needs to include !karma, so you could see karma of other users.
5+
E.g. !karma Hjorth - Would return the total karma of Hjorth no matter who the author is.
6+
Needs to add highscores for both opdutter og neddutter.
7+
Maybe give roles based on these, and let them update once someone else takes the lead.
8+
9+
# POMODORO PART
10+
(X) Pomodoro basic timer funcionality.
11+
(X) Pomodoro makes bot join and play sound when time is up.
12+
(X) Pomodoro default timers hvis andet ikke er specified.
13+
(X) Pomodoro !time så vi kan se hvor lang tid der er tilbage af en timer
14+
Let user change default timers for pomodoro, maybe something like !defaultbreak = something.
15+
User needs to be able to STOP the timer
16+
User needs to be able to PAUSE the timer
17+
18+
# PLAY PART
19+
(X) !play latex bastards - FILE ADDED.
20+
(X) !play bamse - DEN NYE POMODORO LYD.
21+
!play pomodoro lyd start/slut.
22+
(X) Maybe seperate file for alle playing, if it starts to grow any bigger.
23+
Makes the code more reable.
24+
It is growing out of hand, please make a new file that contains the playing part,
25+
so the if statements in bot is not so big.
26+
Maybe it can be abstracted more and the play document can handle which file to play.
27+
if "something" in message.content
28+
Player.Play(message) -> Some if chain in there that does the choosing of what to play.
29+
Only problem right now is that they are all different, !latex, !play, !inspiration etc.
30+
MAYBE !p inspiration, then that would be a lot simpler and a lot less if in the bot file.
31+
!play farvelsangen.
32+
(X) # Figure out some way to make it choose the channel with users in
33+
(X) !Motivationsbot - DU GØR DET OG DU GØR DET GODT.
34+
35+
# DB
36+
Database skal sættes op til at være online - lav prio.
37+
Reset all the data in the database, cause sten's id is different from everyone elses,
38+
and it's pissing me off.
39+
Add collection for work/time.

__pycache__/User.cpython-37.pyc

0 Bytes
Binary file not shown.

bot.py

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
1-
import os
2-
import discord
3-
import pymongo
4-
import User
5-
import Db
6-
import Pomodoro
1+
import os, discord, pymongo, User, Db, threading, time, Pomodoro, Constants, Player
2+
from dotenv import load_dotenv
73

8-
# TODO
9-
# The enviorement file does not work either, so the bot token needs to be refreshed each time it is pushed to git
10-
# Add pomodoro timer
11-
# Add another collection in the database to track how long we are working for, just like lønnes ui in his
12-
13-
# Discord bot token
14-
TOKEN = 'NjkwNTM0MDI4MzE0NjczMTUy.XnzdwQ.xyP92St3lGIaA5IuIGNwHkCJ3lw'
4+
load_dotenv()
155

166
# Emotes
177
kurtApproved = 619818932475527210
@@ -93,20 +83,15 @@ async def on_message(message):
9383
if message.author.id == user.intUserID:
9484
x = Db.mycol.find_one({ "Name": user.name })
9585
await message.channel.send('{} has {} total karma. {} opdutter and {} neddutter'.format(x["Name"], x["Opdutter"] - x["Neddutter"], x["Opdutter"], x["Neddutter"]))
96-
9786

98-
# No checks at the moment, so will likely break with wrong inputs from user.
9987
if "!pomodoro" in message.content:
100-
# Maybe if user is certain role on discord, so not everyone can fuck with this.
88+
await Pomodoro.startTimers(message)
89+
90+
if "!time" in message.content:
91+
await message.channel.send('Remaining time: {}'.format(Pomodoro.calculateRemainingTime()))
10192

102-
# This entire part is coded based on Kurts saying, I wish this was here.
103-
x = message.content
104-
string_pomodoro_time = x[10:]
105-
string_break_time = x[-2:]
106-
pomodoro_time = int(string_pomodoro_time)
107-
break_time = int(string_break_time)
108-
Pomodoro.startPomodoro(pomodoro_time, break_time)
109-
93+
if "!p" in message.content:
94+
await Player.play(message)
11095

11196
# Hidden easter egg for the boys
11297
if message.content == "!bot":
@@ -116,7 +101,8 @@ async def on_message(message):
116101
async def on_ready():
117102
print(f'{client.user} has connected to Discord!')
118103

104+
# Fix db
119105
for document in Db.mycol.find():
120106
print(document)
121107

122-
client.run(TOKEN)
108+
client.run(os.getenv("TOKEN"))

mp3-files/LatexBusters.mp3

229 KB
Binary file not shown.

mp3-files/Ole_Wedel.mp3

218 KB
Binary file not shown.

0 commit comments

Comments
 (0)