Skip to content
This repository was archived by the owner on Mar 31, 2023. It is now read-only.

♻️ Refactor file structure #31

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ Use the `credentials.example.py` as a template.

### 2. Create configuration.py (optional)

Use the `configuration.example.py` as a template.
Use the `configuration.example.py` as templates. Place each `configuration.py` in the same directory as the
`configuration.example.py`.

### Example

Expand All @@ -48,7 +49,7 @@ bot.exit()

## Modules
The project used to be limited to the `TS3Bot` and `TS3Query` classes but as I worked on it,
I added modules I thought others would want to use as well, especially the `move_afk` module.
I added modules I thought others would want to use as well, especially the `afk_mover` module.
<br><br>
The other modules - namely `time_tracker`, `reminder`, `doodle` and `wordpress` -
are rather specific for my circle of friends' TS3 server:
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions Client.py → client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from configuration import IDLE_MINUTES_BEFORE_MOVE, AFK_CHANNELS
from configuration import IDLE_MINUTES_BEFORE_AFK, AFK_CHANNELS


class Client:
Expand All @@ -25,10 +25,10 @@ def is_afk(self):

idle_minutes = self.idle_time / 60

if idle_minutes > IDLE_MINUTES_BEFORE_MOVE or self.is_away:
if idle_minutes > IDLE_MINUTES_BEFORE_AFK or self.is_away:
return True

return self.is_muted and idle_minutes > IDLE_MINUTES_BEFORE_MOVE / 2
return self.is_muted and idle_minutes > IDLE_MINUTES_BEFORE_AFK / 2

@property
def is_muted(self):
Expand Down
60 changes: 3 additions & 57 deletions configuration.example.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,8 @@
BOT_DESC = ''
BOT_AVAT = ''

# Channels
# Channel
AFK_CHANNELS = [345, 567, 678, 789]
BUSY_CHANNEL = 345 # Busy

# Module specifics
WORDPRESS_UPDATE_INTERVAL_SECONDS = 15 * 60
IDLE_MINUTES_BEFORE_MOVE = 20
CHECK_AFK_INTERVAL_SECONDS = 10
MEASUREMENT_INTERVAL_SECONDS = 5
MEASUREMENT_EMPTY_INTERVAL_SECONDS = 5 * 60

AFK_MOVED_MESSAGE = 'You were moved because of inactivity.'

TRACK_TOGGLE_CMD = '!toggle'
TRACKING_INFO_MSG = f'Your connection time is being tracked. To opt-out, enter [b]{TRACK_TOGGLE_CMD}[/b].'
DO_NOT_TRACK_CONFIRMED_MSG = f'Your connection time is not being tracked anymore. ' \
f'[b]{TRACK_TOGGLE_CMD}[/b] to undo.'

CRACKERBARREL_REMINDER_3_DAYS = 'Crackerbarrel meeting in 3 days!'
CRACKERBARREL_REMINDER_TODAY = 'Crackerbarrel meeting today 8pm!'

BANNERS = {
'default': 'https://your-domain.com/files/banner-default.png',
'covid': 'https://your-domain.com/files/banner-corona.png',
'valentines': 'https://your-domain.com/files/banner-valentines.png',
'eastern': 'https://your-domain.com/files/banner-eastern.png',
'april fools': 'https://your-domain.com/files/banner-april-fools.png',
'mothers day': 'https://your-domain.com/files/banner-mothers-day.png',
'fathers day': 'https://your-domain.com/files/banner-fathers-day.png',
'halloween': 'https://your-domain.com/files/banner-halloween.png',
'christmas': 'https://your-domain.com/files/banner-christmas.png',
'new years': 'https://your-domain.com/files/banner-new-year.png',
}


# Games specifics
ACCOUNTS_DB_NAME = 'accounts'
ACCOUNT_START_BALANCE = 5000
ACCOUNT_INFO_BALANCE = 'Your balance is {}.'

GAME_INVALID_CMD = 'Please enter a valid command.'
GAME_INVALID_AMOUNT = 'Please enter a valid amount.'
GAME_INVALID_GAME = 'Please enter a valid game.'
GAME_ABORT_BAD_BET = 'Please enter a valid bet!'
GAME_ABORT_BROKE = 'Sorry, you don\'t have enough money!'
GAME_ABORT_BAD_WAGER = 'Sorry, minimum wager is {}!'
GAME_GREETINGS = 'Hello {}. Thank you for playing!' # player_name
GAME_WON_MSG = 'You win [b][color={0}]{1}[/color][/b]!!' # color, win_amount
GAME_LOST_MSG = 'Oh no! You lost [b][color=firebrick]{}[/color][/b]!' # wager

# Slots
SLOTS_MIN_WAGER = 50
SLOTS_START_MSG = 'You started the slot machine! Good luck!'
SLOTS_RESULT_MSG = 'Your reels show:'

# Roulette
ROULETTE_MIN_WAGER = 50
ROULETTE_START_MSG = 'You bet [b]{0}[/b] on {1}! Let\'s spin the wheel!' # amount, number or name
ROULETTE_RESULT_MSG = 'The ball lands on [b]{}[/b]!!' # winning_number
# Client
IDLE_MINUTES_BEFORE_AFK = 30
30 changes: 17 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import threading

from configuration import BOT_NAME, BOT_DESC, ACCOUNTS_DB_NAME, PROFILES_DB_NAME
from configuration import BOT_NAME, BOT_DESC
from credentials import *

from modules.reminder import crackerbarrel_reminder
from modules.doodle import set_holiday_doodle
from modules.move_afk import move_afk
from modules.reminder import reminder
from modules.doodle import doodle
from modules.afk_mover import afk_mover

from modules.games import games, AccountDB
from modules.games.configuration import ACCOUNTS_DB_NAME

from modules.time_tracker import time_tracker, wordpress, ProfilesDB, WordpressDB
from modules.time_tracker.configuration import PROFILES_DB_NAME

from TS3Bot import TS3Bot
from ts3bot import TS3Bot
from ts3query import TS3Query


def main():
bot = TS3Bot(ip=SERVER_IP,
query = TS3Query(ip=SERVER_IP, port=TELNET_PORT)
bot = TS3Bot(query=query,
port=SERVER_PORT,
login=TELNET_LOGIN,
password=TELNET_PW,
telnet_port=TELNET_PORT)
password=TELNET_PW)

bot.set_bot_name(BOT_NAME)
bot.set_bot_description(BOT_DESC)
Expand All @@ -28,23 +32,23 @@ def main():
profile_db = ProfilesDB(PROFILES_DB_NAME)
wordpress_db = WordpressDB(MYSQL_HOST, MYSQL_DB_NAME, MYSQL_USER, MYSQL_PW, 'stats')

crackerbarrel_reminder_thread = threading.Thread(target=crackerbarrel_reminder, kwargs={'bot': bot})
holiday_doodle_thread = threading.Thread(target=set_holiday_doodle, kwargs={'bot': bot})
move_afk_thread = threading.Thread(target=move_afk, kwargs={'bot': bot})
crackerbarrel_reminder_thread = threading.Thread(target=reminder.check_crackerbarrel_reminder, kwargs={'bot': bot})
holiday_doodle_thread = threading.Thread(target=doodle.set_holiday_doodle, kwargs={'bot': bot})
move_afk_thread = threading.Thread(target=afk_mover.move_afk, kwargs={'bot': bot})

games_thread = threading.Thread(target=games.start,
kwargs={
'bot': bot,
'database': accounts_db,
})

time_measurement_thread = threading.Thread(target=time_tracker.start,
time_measurement_thread = threading.Thread(target=time_tracker.start_tracker,
kwargs={
'bot': bot,
'database': profile_db,
})

wordpress_update_thread = threading.Thread(target=wordpress.update,
wordpress_update_thread = threading.Thread(target=wordpress.update_post,
kwargs={
'profile_db': profile_db,
'wordpress_db': wordpress_db,
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions modules/move_afk.py → modules/afk_mover/afk_mover.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import time
from TS3Bot import TS3Bot
from configuration import BUSY_CHANNEL, CHECK_AFK_INTERVAL_SECONDS, AFK_MOVED_MESSAGE
from ts3bot import TS3Bot
from .configuration import BUSY_CHANNEL, CHECK_AFK_INTERVAL_SECONDS, AFK_MOVED_MESSAGE


def move_afk(bot: TS3Bot):
Expand Down
3 changes: 3 additions & 0 deletions modules/afk_mover/configuration.example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
AFK_MOVED_MESSAGE = 'You were moved because of inactivity.'
BUSY_CHANNEL = 345 # Busy
CHECK_AFK_INTERVAL_SECONDS = 10
12 changes: 12 additions & 0 deletions modules/doodle/configuration.example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
BANNERS = {
'default': 'https://your-domain.com/files/banner-default.png',
'covid': 'https://your-domain.com/files/banner-corona.png',
'valentines': 'https://your-domain.com/files/banner-valentines.png',
'eastern': 'https://your-domain.com/files/banner-eastern.png',
'april fools': 'https://your-domain.com/files/banner-april-fools.png',
'mothers day': 'https://your-domain.com/files/banner-mothers-day.png',
'fathers day': 'https://your-domain.com/files/banner-fathers-day.png',
'halloween': 'https://your-domain.com/files/banner-halloween.png',
'christmas': 'https://your-domain.com/files/banner-christmas.png',
'new years': 'https://your-domain.com/files/banner-new-year.png',
}
6 changes: 3 additions & 3 deletions modules/doodle.py → modules/doodle/doodle.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime

from TS3Bot import TS3Bot
from configuration import BANNERS
from ts3bot import TS3Bot
from .configuration import BANNERS


def set_holiday_doodle(bot: TS3Bot):
Expand All @@ -12,7 +12,7 @@ def set_holiday_doodle(bot: TS3Bot):
return bot.change_host_banner_image(BANNERS['april fools'])
elif today.month == 5 and 7 <= today.day <= 15 and today.weekday() == 6:
return bot.change_host_banner_image(BANNERS['mothers day'])
elif today.month == 10 and 24 <= today.day <= 31:
elif today.month == 10 and 27 <= today.day <= 31:
return bot.change_host_banner_image(BANNERS['halloween'])
elif today.month == 12 and 1 <= today.day <= 26:
return bot.change_host_banner_image(BANNERS['christmas'])
Expand Down
2 changes: 1 addition & 1 deletion modules/games/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from modules.games.AccountDB import AccountDB
from modules.games.accounts_db import AccountDB
4 changes: 2 additions & 2 deletions modules/games/AccountDB.py → modules/games/accounts_db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from sqlite3 import OperationalError
from SQLiteDB import SQLiteDB
from configuration import ACCOUNT_START_BALANCE
from sqlite_db import SQLiteDB
from .configuration import ACCOUNT_START_BALANCE


class AccountDB(SQLiteDB):
Expand Down
23 changes: 23 additions & 0 deletions modules/games/configuration.example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
ACCOUNTS_DB_NAME = 'accounts'
ACCOUNT_START_BALANCE = 5000
ACCOUNT_INFO_BALANCE = 'Your balance is {}.'

GAME_INVALID_CMD = 'Please enter a valid command.'
GAME_INVALID_AMOUNT = 'Please enter a valid amount.'
GAME_INVALID_GAME = 'Please enter a valid game.'
GAME_ABORT_BAD_BET = 'Please enter a valid bet!'
GAME_ABORT_BROKE = 'Sorry, you don\'t have enough money!'
GAME_ABORT_BAD_WAGER = 'Sorry, minimum wager is {}!'
GAME_GREETINGS = 'Hello {}. Thank you for playing!' # player_name
GAME_WON_MSG = 'You win [b][color={0}]{1}[/color][/b]!!' # color, win_amount
GAME_LOST_MSG = 'Oh no! You lost [b][color=firebrick]{}[/color][/b]!' # wager

# Slots
SLOTS_MIN_WAGER = 50
SLOTS_START_MSG = 'You started the slot machine! Good luck!'
SLOTS_RESULT_MSG = 'Your reels show:'

# Roulette
ROULETTE_MIN_WAGER = 50
ROULETTE_START_MSG = 'You bet [b]{0}[/b] on {1}! Let\'s spin the wheel!' # amount, number or name
ROULETTE_RESULT_MSG = 'The ball lands on [b]{}[/b]!!' # winning_number
12 changes: 6 additions & 6 deletions modules/games/games.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import time
import threading

from configuration import GAME_GREETINGS, GAME_INVALID_CMD, GAME_INVALID_AMOUNT, GAME_INVALID_GAME, \
from .configuration import GAME_GREETINGS, GAME_INVALID_CMD, GAME_INVALID_AMOUNT, GAME_INVALID_GAME, \
GAME_ABORT_BROKE, GAME_ABORT_BAD_WAGER, GAME_ABORT_BAD_BET, ACCOUNT_INFO_BALANCE

from modules.games.Slots import Slots
from modules.games.Roulette import Roulette
from modules.games.AccountDB import AccountDB
from modules.games.slots import Slots
from modules.games.roulette import Roulette
from modules.games.accounts_db import AccountDB

from Message import Message
from TS3Bot import TS3Bot
from message import Message
from ts3bot import TS3Bot

GAMES = ['slots', 'roulette']

Expand Down
4 changes: 2 additions & 2 deletions modules/games/Roulette.py → modules/games/roulette.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import random
import time

from configuration import ROULETTE_MIN_WAGER, ROULETTE_START_MSG, ROULETTE_RESULT_MSG, GAME_LOST_MSG, GAME_WON_MSG
from .configuration import ROULETTE_MIN_WAGER, ROULETTE_START_MSG, ROULETTE_RESULT_MSG, GAME_LOST_MSG, GAME_WON_MSG
from modules.games.formatting import get_color
from TS3Bot import TS3Bot
from ts3bot import TS3Bot

BETS = {
'low': {'name': 'Low',
Expand Down
4 changes: 2 additions & 2 deletions modules/games/Slots.py → modules/games/slots.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import random
import time

from configuration import GAME_WON_MSG, GAME_LOST_MSG, SLOTS_MIN_WAGER, SLOTS_START_MSG, SLOTS_RESULT_MSG
from .configuration import GAME_WON_MSG, GAME_LOST_MSG, SLOTS_MIN_WAGER, SLOTS_START_MSG, SLOTS_RESULT_MSG
from modules.games.formatting import get_color
from TS3Bot import TS3Bot
from ts3bot import TS3Bot


REEL_OPTIONS = [option for option in enumerate([
Expand Down
2 changes: 2 additions & 0 deletions modules/reminder/configuration.example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
REMINDER_3_DAYS_MSG = 'Crackerbarrel meeting in 3 days!'
REMINDER_TODAY_MSG = 'Crackerbarrel meeting today 8pm!'
10 changes: 5 additions & 5 deletions modules/reminder.py → modules/reminder/reminder.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import calendar
import datetime

from TS3Bot import TS3Bot
from configuration import CRACKERBARREL_REMINDER_3_DAYS, CRACKERBARREL_REMINDER_TODAY
from ts3bot import TS3Bot
from .configuration import REMINDER_3_DAYS_MSG, REMINDER_TODAY_MSG

days = {
'Monday': 0,
Expand All @@ -15,17 +15,17 @@
}


def crackerbarrel_reminder(bot: TS3Bot):
def check_crackerbarrel_reminder(bot: TS3Bot):
today = datetime.datetime.today()
wednesday = days['Wednesday']
last_wednesday = last_weekday_of_month(wednesday, today.year, today.month)

if today.day == last_wednesday:
bot.set_host_message_mode(2)
return bot.set_new_host_message(CRACKERBARREL_REMINDER_TODAY)
return bot.set_new_host_message(REMINDER_TODAY_MSG)
elif today.day == last_wednesday - 3:
bot.set_host_message_mode(2)
return bot.set_new_host_message(CRACKERBARREL_REMINDER_3_DAYS)
return bot.set_new_host_message(REMINDER_3_DAYS_MSG)
elif today.day == last_wednesday + 1:
return bot.reset_host_message()

Expand Down
4 changes: 2 additions & 2 deletions modules/time_tracker/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from modules.time_tracker.ProfilesDB import ProfilesDB
from modules.time_tracker.WordpressDB import WordpressDB
from modules.time_tracker.profiles_db import ProfilesDB
from modules.time_tracker.wordpress_db import WordpressDB
11 changes: 11 additions & 0 deletions modules/time_tracker/configuration.example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Tracking
PROFILES_DB_NAME = 'profiles'
MEASUREMENT_INTERVAL_SECONDS = 5
MEASUREMENT_EMPTY_INTERVAL_SECONDS = 5 * 60
TRACK_TOGGLE_CMD = '!toggle'
TRACKING_INFO_MSG = f'Your connection time is being tracked. To opt-out, enter [b]{TRACK_TOGGLE_CMD}[/b].'
DO_NOT_TRACK_CONFIRMED_MSG = f'Your connection time is not being tracked anymore. ' \
f'[b]{TRACK_TOGGLE_CMD}[/b] to undo.'

# WordPress
WORDPRESS_UPDATE_INTERVAL_SECONDS = 15 * 60
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from sqlite3 import OperationalError
import time

from Client import Client
from SQLiteDB import SQLiteDB
from client import Client
from sqlite_db import SQLiteDB


class ProfilesDB(SQLiteDB):
Expand Down
Loading