KiTTY, un bot Discord qui est un petit chat :) Il est basé sur une ancienne version du bot Red, sous Python 3.6 et qui a des fonctionnalités bien sympatiques !
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Deze repo is gearchiveerd. U kunt bestanden bekijken en het klonen, maar niet pushen of problemen/pull-requests openen.

740 regels
28 KiB

  1. import discord
  2. from discord.ext import commands
  3. from cogs.utils.dataIO import dataIO
  4. from collections import namedtuple, defaultdict, deque
  5. from datetime import datetime
  6. from copy import deepcopy
  7. from .utils import checks
  8. from cogs.utils.chat_formatting import pagify, box
  9. from enum import Enum
  10. from __main__ import send_cmd_help
  11. import os
  12. import time
  13. import logging
  14. import random
  15. default_settings = {"PAYDAY_TIME": 300, "PAYDAY_CREDITS": 120,
  16. "SLOT_MIN": 1, "SLOT_MAX": 1000, "SLOT_TIME": 0,
  17. "REGISTER_CREDITS": 0}
  18. class EconomyError(Exception):
  19. pass
  20. class OnCooldown(EconomyError):
  21. pass
  22. class InvalidBid(EconomyError):
  23. pass
  24. class BankError(Exception):
  25. pass
  26. class AccountAlreadyExists(BankError):
  27. pass
  28. class NoAccount(BankError):
  29. pass
  30. class InsufficientBalance(BankError):
  31. pass
  32. class NegativeValue(BankError):
  33. pass
  34. class SameSenderAndReceiver(BankError):
  35. pass
  36. NUM_ENC = "\N{COMBINING ENCLOSING KEYCAP}"
  37. class SMReel(Enum):
  38. cherries = "\N{CHERRIES}"
  39. cookie = "\N{COOKIE}"
  40. two = "\N{DIGIT TWO}" + NUM_ENC
  41. flc = "\N{FOUR LEAF CLOVER}"
  42. cyclone = "\N{CYCLONE}"
  43. sunflower = "\N{SUNFLOWER}"
  44. six = "\N{DIGIT SIX}" + NUM_ENC
  45. mushroom = "\N{MUSHROOM}"
  46. heart = "\N{HEAVY BLACK HEART}"
  47. snowflake = "\N{SNOWFLAKE}"
  48. PAYOUTS = {
  49. (SMReel.two, SMReel.two, SMReel.six) : {
  50. "payout" : lambda x: x * 2500 + x,
  51. "phrase" : "JACKPOT! 226! Your bid has been multiplied * 2500! :cat:"
  52. },
  53. (SMReel.flc, SMReel.flc, SMReel.flc) : {
  54. "payout" : lambda x: x + 1000,
  55. "phrase" : "4LC! +1000!"
  56. },
  57. (SMReel.cherries, SMReel.cherries, SMReel.cherries) : {
  58. "payout" : lambda x: x + 800,
  59. "phrase" : "Three cherries! +800!"
  60. },
  61. (SMReel.two, SMReel.six) : {
  62. "payout" : lambda x: x * 4 + x,
  63. "phrase" : "2 6! Your bid has been multiplied * 4! :scream_cat:"
  64. },
  65. (SMReel.cherries, SMReel.cherries) : {
  66. "payout" : lambda x: x * 3 + x,
  67. "phrase" : "Two cherries! Your bid has been multiplied * 3!"
  68. },
  69. "3 symbols" : {
  70. "payout" : lambda x: x + 500,
  71. "phrase" : "Three symbols! +500!"
  72. },
  73. "2 symbols" : {
  74. "payout" : lambda x: x * 2 + x,
  75. "phrase" : "Two consecutive symbols! Your bid has been multiplied * 2!"
  76. },
  77. }
  78. SLOT_PAYOUTS_MSG = ("Slot machine payouts:\n"
  79. "{two.value} {two.value} {six.value} Bet * 2500\n"
  80. "{flc.value} {flc.value} {flc.value} +1000\n"
  81. "{cherries.value} {cherries.value} {cherries.value} +800\n"
  82. "{two.value} {six.value} Bet * 4\n"
  83. "{cherries.value} {cherries.value} Bet * 3\n\n"
  84. "Three symbols: +500\n"
  85. "Two symbols: Bet * 2".format(**SMReel.__dict__))
  86. class Bank:
  87. def __init__(self, bot, file_path):
  88. self.accounts = dataIO.load_json(file_path)
  89. self.bot = bot
  90. def create_account(self, user, *, initial_balance=0):
  91. server = user.server
  92. if not self.account_exists(user):
  93. if server.id not in self.accounts:
  94. self.accounts[server.id] = {}
  95. if user.id in self.accounts: # Legacy account
  96. balance = self.accounts[user.id]["balance"]
  97. else:
  98. balance = initial_balance
  99. timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
  100. account = {"name": user.name,
  101. "balance": balance,
  102. "created_at": timestamp
  103. }
  104. self.accounts[server.id][user.id] = account
  105. self._save_bank()
  106. return self.get_account(user)
  107. else:
  108. raise AccountAlreadyExists()
  109. def account_exists(self, user):
  110. try:
  111. self._get_account(user)
  112. except NoAccount:
  113. return False
  114. return True
  115. def withdraw_credits(self, user, amount):
  116. server = user.server
  117. if amount < 0:
  118. raise NegativeValue()
  119. account = self._get_account(user)
  120. if account["balance"] >= amount:
  121. account["balance"] -= amount
  122. self.accounts[server.id][user.id] = account
  123. self._save_bank()
  124. else:
  125. raise InsufficientBalance()
  126. def deposit_credits(self, user, amount):
  127. server = user.server
  128. if amount < 0:
  129. raise NegativeValue()
  130. account = self._get_account(user)
  131. account["balance"] += amount
  132. self.accounts[server.id][user.id] = account
  133. self._save_bank()
  134. def set_credits(self, user, amount):
  135. server = user.server
  136. if amount < 0:
  137. raise NegativeValue()
  138. account = self._get_account(user)
  139. account["balance"] = amount
  140. self.accounts[server.id][user.id] = account
  141. self._save_bank()
  142. def transfer_credits(self, sender, receiver, amount):
  143. if amount < 0:
  144. raise NegativeValue()
  145. if sender is receiver:
  146. raise SameSenderAndReceiver()
  147. if self.account_exists(sender) and self.account_exists(receiver):
  148. sender_acc = self._get_account(sender)
  149. if sender_acc["balance"] < amount:
  150. raise InsufficientBalance()
  151. self.withdraw_credits(sender, amount)
  152. self.deposit_credits(receiver, amount)
  153. else:
  154. raise NoAccount()
  155. def can_spend(self, user, amount):
  156. account = self._get_account(user)
  157. if account["balance"] >= amount:
  158. return True
  159. else:
  160. return False
  161. def wipe_bank(self, server):
  162. self.accounts[server.id] = {}
  163. self._save_bank()
  164. def get_server_accounts(self, server):
  165. if server.id in self.accounts:
  166. raw_server_accounts = deepcopy(self.accounts[server.id])
  167. accounts = []
  168. for k, v in raw_server_accounts.items():
  169. v["id"] = k
  170. v["server"] = server
  171. acc = self._create_account_obj(v)
  172. accounts.append(acc)
  173. return accounts
  174. else:
  175. return []
  176. def get_all_accounts(self):
  177. accounts = []
  178. for server_id, v in self.accounts.items():
  179. server = self.bot.get_server(server_id)
  180. if server is None:
  181. # Servers that have since been left will be ignored
  182. # Same for users_id from the old bank format
  183. continue
  184. raw_server_accounts = deepcopy(self.accounts[server.id])
  185. for k, v in raw_server_accounts.items():
  186. v["id"] = k
  187. v["server"] = server
  188. acc = self._create_account_obj(v)
  189. accounts.append(acc)
  190. return accounts
  191. def get_balance(self, user):
  192. account = self._get_account(user)
  193. return account["balance"]
  194. def get_account(self, user):
  195. acc = self._get_account(user)
  196. acc["id"] = user.id
  197. acc["server"] = user.server
  198. return self._create_account_obj(acc)
  199. def _create_account_obj(self, account):
  200. account["member"] = account["server"].get_member(account["id"])
  201. account["created_at"] = datetime.strptime(account["created_at"],
  202. "%Y-%m-%d %H:%M:%S")
  203. Account = namedtuple("Account", "id name balance "
  204. "created_at server member")
  205. return Account(**account)
  206. def _save_bank(self):
  207. dataIO.save_json("data/economy/bank.json", self.accounts)
  208. def _get_account(self, user):
  209. server = user.server
  210. try:
  211. return deepcopy(self.accounts[server.id][user.id])
  212. except KeyError:
  213. raise NoAccount()
  214. class SetParser:
  215. def __init__(self, argument):
  216. allowed = ("+", "-")
  217. if argument and argument[0] in allowed:
  218. try:
  219. self.sum = int(argument)
  220. except:
  221. raise
  222. if self.sum < 0:
  223. self.operation = "withdraw"
  224. elif self.sum > 0:
  225. self.operation = "deposit"
  226. else:
  227. raise
  228. self.sum = abs(self.sum)
  229. elif argument.isdigit():
  230. self.sum = int(argument)
  231. self.operation = "set"
  232. else:
  233. raise
  234. class Economy:
  235. """Economy
  236. Get rich and have fun with imaginary currency!"""
  237. def __init__(self, bot):
  238. global default_settings
  239. self.bot = bot
  240. self.bank = Bank(bot, "data/economy/bank.json")
  241. self.file_path = "data/economy/settings.json"
  242. self.settings = dataIO.load_json(self.file_path)
  243. if "PAYDAY_TIME" in self.settings: # old format
  244. default_settings = self.settings
  245. self.settings = {}
  246. self.settings = defaultdict(default_settings.copy, self.settings)
  247. self.payday_register = defaultdict(dict)
  248. self.slot_register = defaultdict(dict)
  249. @commands.group(name="bank", pass_context=True)
  250. async def _bank(self, ctx):
  251. """Bank operations"""
  252. if ctx.invoked_subcommand is None:
  253. await send_cmd_help(ctx)
  254. @_bank.command(pass_context=True, no_pm=True)
  255. async def register(self, ctx):
  256. """Registers an account at the Twentysix bank"""
  257. settings = self.settings[ctx.message.server.id]
  258. author = ctx.message.author
  259. credits = 0
  260. if ctx.message.server.id in self.settings:
  261. credits = settings.get("REGISTER_CREDITS", 0)
  262. try:
  263. account = self.bank.create_account(author, initial_balance=credits)
  264. await self.bot.say("{} Account opened. Current balance: {}"
  265. "".format(author.mention, account.balance))
  266. except AccountAlreadyExists:
  267. await self.bot.say("{} You already have an account at the"
  268. " KiTTYs bank.".format(author.mention))
  269. @_bank.command(pass_context=True)
  270. async def balance(self, ctx, user: discord.Member=None):
  271. """Shows balance of user.
  272. Defaults to yours."""
  273. if not user:
  274. user = ctx.message.author
  275. try:
  276. await self.bot.say("{} Your balance is: {}".format(
  277. user.mention, self.bank.get_balance(user)))
  278. except NoAccount:
  279. await self.bot.say("{} You don't have an account at the"
  280. " KiTTYs bank. Type `{}bank register`"
  281. " to open one.".format(user.mention,
  282. ctx.prefix))
  283. else:
  284. try:
  285. await self.bot.say("{}'s balance is {}".format(
  286. user.name, self.bank.get_balance(user)))
  287. except NoAccount:
  288. await self.bot.say("That user has no bank account.")
  289. @_bank.command(pass_context=True)
  290. async def transfer(self, ctx, user: discord.Member, sum: int):
  291. """Transfer credits to other users"""
  292. author = ctx.message.author
  293. try:
  294. self.bank.transfer_credits(author, user, sum)
  295. logger.info("{}({}) transferred {} credits to {}({})".format(
  296. author.name, author.id, sum, user.name, user.id))
  297. await self.bot.say("{} credits have been transferred to {}'s"
  298. " account.".format(sum, user.name))
  299. except NegativeValue:
  300. await self.bot.say("You need to transfer at least 1 credit.")
  301. except SameSenderAndReceiver:
  302. await self.bot.say("You can't transfer credits to yourself.")
  303. except InsufficientBalance:
  304. await self.bot.say("You don't have that sum in your bank account.")
  305. except NoAccount:
  306. await self.bot.say("That user has no bank account.")
  307. @_bank.command(name="set", pass_context=True)
  308. @checks.admin_or_permissions(manage_server=True)
  309. async def _set(self, ctx, user: discord.Member, credits: SetParser):
  310. """Sets credits of user's bank account. See help for more operations
  311. Passing positive and negative values will add/remove credits instead
  312. Examples:
  313. bank set @Twentysix 26 - Sets 26 credits
  314. bank set @Twentysix +2 - Adds 2 credits
  315. bank set @Twentysix -6 - Removes 6 credits"""
  316. author = ctx.message.author
  317. try:
  318. if credits.operation == "deposit":
  319. self.bank.deposit_credits(user, credits.sum)
  320. logger.info("{}({}) added {} credits to {} ({})".format(
  321. author.name, author.id, credits.sum, user.name, user.id))
  322. await self.bot.say("{} credits have been added to {}"
  323. "".format(credits.sum, user.name))
  324. elif credits.operation == "withdraw":
  325. self.bank.withdraw_credits(user, credits.sum)
  326. logger.info("{}({}) removed {} credits to {} ({})".format(
  327. author.name, author.id, credits.sum, user.name, user.id))
  328. await self.bot.say("{} credits have been withdrawn from {}"
  329. "".format(credits.sum, user.name))
  330. elif credits.operation == "set":
  331. self.bank.set_credits(user, credits.sum)
  332. logger.info("{}({}) set {} credits to {} ({})"
  333. "".format(author.name, author.id, credits.sum,
  334. user.name, user.id))
  335. await self.bot.say("{}'s credits have been set to {}".format(
  336. user.name, credits.sum))
  337. except InsufficientBalance:
  338. await self.bot.say("User doesn't have enough credits.")
  339. except NoAccount:
  340. await self.bot.say("User has no bank account.")
  341. @_bank.command(pass_context=True, no_pm=True)
  342. @checks.serverowner_or_permissions(administrator=True)
  343. async def reset(self, ctx, confirmation: bool=False):
  344. """Deletes all server's bank accounts"""
  345. if confirmation is False:
  346. await self.bot.say("This will delete all bank accounts on "
  347. "this server.\nIf you're sure, type "
  348. "{}bank reset yes".format(ctx.prefix))
  349. else:
  350. self.bank.wipe_bank(ctx.message.server)
  351. await self.bot.say("All bank accounts of this server have been "
  352. "deleted.")
  353. @commands.command(pass_context=True, no_pm=True)
  354. async def daily(self, ctx): # TODO
  355. """Get some free credits"""
  356. author = ctx.message.author
  357. server = author.server
  358. id = author.id
  359. if self.bank.account_exists(author):
  360. if id in self.payday_register[server.id]:
  361. seconds = abs(self.payday_register[server.id][
  362. id] - int(time.perf_counter()))
  363. if seconds >= self.settings[server.id]["PAYDAY_TIME"]:
  364. self.bank.deposit_credits(author, self.settings[
  365. server.id]["PAYDAY_CREDITS"])
  366. self.payday_register[server.id][
  367. id] = int(time.perf_counter())
  368. await self.bot.say(
  369. "{} Here, take some credits. Enjoy! (+{}"
  370. " credits!)".format(
  371. author.mention,
  372. str(self.settings[server.id]["PAYDAY_CREDITS"])))
  373. else:
  374. dtime = self.display_time(
  375. self.settings[server.id]["PAYDAY_TIME"] - seconds)
  376. await self.bot.say(
  377. "{} Too soon. For your next payday you have to"
  378. " wait {}.".format(author.mention, dtime))
  379. else:
  380. self.payday_register[server.id][id] = int(time.perf_counter())
  381. self.bank.deposit_credits(author, self.settings[
  382. server.id]["PAYDAY_CREDITS"])
  383. await self.bot.say(
  384. "{} Here, take some credits. Enjoy! (+{} credits!)".format(
  385. author.mention,
  386. str(self.settings[server.id]["PAYDAY_CREDITS"])))
  387. else:
  388. await self.bot.say("{} You need an account to receive credits."
  389. " Type `{}bank register` to open one.".format(
  390. author.mention, ctx.prefix))
  391. @commands.group(pass_context=True)
  392. async def leaderboard(self, ctx):
  393. """Server / global leaderboard
  394. Defaults to \"server\" if not issued in DM"""
  395. if ctx.invoked_subcommand is None:
  396. if ctx.message.server:
  397. await ctx.invoke(self._server_leaderboard)
  398. else:
  399. await ctx.invoke(self._global_leaderboard)
  400. @leaderboard.command(name="server", pass_context=True, no_pm=True)
  401. async def _server_leaderboard(self, ctx, top: int=10):
  402. """Prints out the server's leaderboard
  403. Defaults to top 10"""
  404. # Originally coded by Airenkun - edited by irdumb
  405. server = ctx.message.server
  406. if top < 1:
  407. top = 10
  408. bank_sorted = sorted(self.bank.get_server_accounts(server),
  409. key=lambda x: x.balance, reverse=True)
  410. bank_sorted = [a for a in bank_sorted if a.member] # exclude users who left
  411. if len(bank_sorted) < top:
  412. top = len(bank_sorted)
  413. topten = bank_sorted[:top]
  414. highscore = ""
  415. place = 1
  416. for acc in topten:
  417. highscore += str(place).ljust(len(str(top)) + 1)
  418. highscore += (str(acc.member.display_name) + " ").ljust(23 - len(str(acc.balance)))
  419. highscore += str(acc.balance) + "\n"
  420. place += 1
  421. if highscore != "":
  422. for page in pagify(highscore, shorten_by=12):
  423. await self.bot.say(box(page, lang="py"))
  424. else:
  425. await self.bot.say("There are no accounts in the bank. Be the first with `{}bank register`")
  426. @leaderboard.command(name="global")
  427. async def _global_leaderboard(self, top: int=10):
  428. """Prints out the global leaderboard
  429. Defaults to top 10"""
  430. if top < 1:
  431. top = 10
  432. bank_sorted = sorted(self.bank.get_all_accounts(),
  433. key=lambda x: x.balance, reverse=True)
  434. bank_sorted = [a for a in bank_sorted if a.member] # exclude users who left
  435. unique_accounts = []
  436. for acc in bank_sorted:
  437. if not self.already_in_list(unique_accounts, acc):
  438. unique_accounts.append(acc)
  439. if len(unique_accounts) < top:
  440. top = len(unique_accounts)
  441. topten = unique_accounts[:top]
  442. highscore = ""
  443. place = 1
  444. for acc in topten:
  445. highscore += str(place).ljust(len(str(top)) + 1)
  446. highscore += ("{} |{}| ".format(acc.member, acc.server)
  447. ).ljust(23 - len(str(acc.balance)))
  448. highscore += str(acc.balance) + "\n"
  449. place += 1
  450. if highscore != "":
  451. for page in pagify(highscore, shorten_by=12):
  452. await self.bot.say(box(page, lang="py"))
  453. else:
  454. await self.bot.say("There are no accounts in the bank. Be the first with `{}bank register`")
  455. def already_in_list(self, accounts, user):
  456. for acc in accounts:
  457. if user.id == acc.id:
  458. return True
  459. return False
  460. @commands.command()
  461. async def payouts(self):
  462. """Shows slot machine payouts"""
  463. await self.bot.whisper(SLOT_PAYOUTS_MSG)
  464. @commands.command(pass_context=True, no_pm=True)
  465. async def slot(self, ctx, bid: int):
  466. """Play the slot machine"""
  467. author = ctx.message.author
  468. server = author.server
  469. settings = self.settings[server.id]
  470. valid_bid = settings["SLOT_MIN"] <= bid and bid <= settings["SLOT_MAX"]
  471. slot_time = settings["SLOT_TIME"]
  472. last_slot = self.slot_register.get(author.id)
  473. now = datetime.utcnow()
  474. try:
  475. if last_slot:
  476. if (now - last_slot).seconds < slot_time:
  477. raise OnCooldown()
  478. if not valid_bid:
  479. raise InvalidBid()
  480. if not self.bank.can_spend(author, bid):
  481. raise InsufficientBalance
  482. await self.slot_machine(author, bid)
  483. except NoAccount:
  484. await self.bot.say("{} You need an account to use the slot "
  485. "machine. Type `{}bank register` to open one."
  486. "".format(author.mention, ctx.prefix))
  487. except InsufficientBalance:
  488. await self.bot.say("{} You need an account with enough funds to "
  489. "play with the slot machine.".format(author.mention))
  490. except OnCooldown:
  491. await self.bot.say("Slot machine is still cooling off! Wait {} "
  492. "seconds between each pull !".format(slot_time))
  493. except InvalidBid:
  494. await self.bot.say("Bid must be between {} and {}."
  495. "".format(settings["SLOT_MIN"],
  496. settings["SLOT_MAX"]))
  497. async def slot_machine(self, author, bid):
  498. default_reel = deque(SMReel)
  499. reels = []
  500. self.slot_register[author.id] = datetime.utcnow()
  501. for i in range(3):
  502. default_reel.rotate(random.randint(-999, 999)) # weeeeee
  503. new_reel = deque(default_reel, maxlen=3) # we need only 3 symbols
  504. reels.append(new_reel) # for each reel
  505. rows = ((reels[0][0], reels[1][0], reels[2][0]),
  506. (reels[0][1], reels[1][1], reels[2][1]),
  507. (reels[0][2], reels[1][2], reels[2][2]))
  508. slot = "~~\n~~" # Mobile friendly
  509. for i, row in enumerate(rows): # Let's build the slot to show
  510. sign = " "
  511. if i == 1:
  512. sign = ">"
  513. slot += "{}{} {} {}\n".format(sign, *[c.value for c in row])
  514. payout = PAYOUTS.get(rows[1])
  515. if not payout:
  516. # Checks for two-consecutive-symbols special rewards
  517. payout = PAYOUTS.get((rows[1][0], rows[1][1]),
  518. PAYOUTS.get((rows[1][1], rows[1][2]))
  519. )
  520. if not payout:
  521. # Still nothing. Let's check for 3 generic same symbols
  522. # or 2 consecutive symbols
  523. has_three = rows[1][0] == rows[1][1] == rows[1][2]
  524. has_two = (rows[1][0] == rows[1][1]) or (rows[1][1] == rows[1][2])
  525. if has_three:
  526. payout = PAYOUTS["3 symbols"]
  527. elif has_two:
  528. payout = PAYOUTS["2 symbols"]
  529. if payout:
  530. then = self.bank.get_balance(author)
  531. pay = payout["payout"](bid)
  532. now = then - bid + pay
  533. self.bank.set_credits(author, now)
  534. await self.bot.say("{}\n{} {}\n\nYour bid: {}\n{} → {}!"
  535. "".format(slot, author.mention,
  536. payout["phrase"], bid, then, now))
  537. else:
  538. then = self.bank.get_balance(author)
  539. self.bank.withdraw_credits(author, bid)
  540. now = then - bid
  541. await self.bot.say("{}\n{} Nothing!\nYour bid: {}\n{} → {}!"
  542. "".format(slot, author.mention, bid, then, now))
  543. @commands.group(pass_context=True, no_pm=True)
  544. @checks.admin_or_permissions(manage_server=True)
  545. async def economyset(self, ctx):
  546. """Changes economy module settings"""
  547. server = ctx.message.server
  548. settings = self.settings[server.id]
  549. if ctx.invoked_subcommand is None:
  550. msg = "```"
  551. for k, v in settings.items():
  552. msg += "{}: {}\n".format(k, v)
  553. msg += "```"
  554. await send_cmd_help(ctx)
  555. await self.bot.say(msg)
  556. @economyset.command(pass_context=True)
  557. async def slotmin(self, ctx, bid: int):
  558. """Minimum slot machine bid"""
  559. server = ctx.message.server
  560. self.settings[server.id]["SLOT_MIN"] = bid
  561. await self.bot.say("Minimum bid is now {} credits.".format(bid))
  562. dataIO.save_json(self.file_path, self.settings)
  563. @economyset.command(pass_context=True)
  564. async def slotmax(self, ctx, bid: int):
  565. """Maximum slot machine bid"""
  566. server = ctx.message.server
  567. self.settings[server.id]["SLOT_MAX"] = bid
  568. await self.bot.say("Maximum bid is now {} credits.".format(bid))
  569. dataIO.save_json(self.file_path, self.settings)
  570. @economyset.command(pass_context=True)
  571. async def slottime(self, ctx, seconds: int):
  572. """Seconds between each slots use"""
  573. server = ctx.message.server
  574. self.settings[server.id]["SLOT_TIME"] = seconds
  575. await self.bot.say("Cooldown is now {} seconds.".format(seconds))
  576. dataIO.save_json(self.file_path, self.settings)
  577. @economyset.command(pass_context=True)
  578. async def paydaytime(self, ctx, seconds: int):
  579. """Seconds between each payday"""
  580. server = ctx.message.server
  581. self.settings[server.id]["PAYDAY_TIME"] = seconds
  582. await self.bot.say("Value modified. At least {} seconds must pass "
  583. "between each payday.".format(seconds))
  584. dataIO.save_json(self.file_path, self.settings)
  585. @economyset.command(pass_context=True)
  586. async def paydaycredits(self, ctx, credits: int):
  587. """Credits earned each payday"""
  588. server = ctx.message.server
  589. self.settings[server.id]["PAYDAY_CREDITS"] = credits
  590. await self.bot.say("Every payday will now give {} credits."
  591. "".format(credits))
  592. dataIO.save_json(self.file_path, self.settings)
  593. @economyset.command(pass_context=True)
  594. async def registercredits(self, ctx, credits: int):
  595. """Credits given on registering an account"""
  596. server = ctx.message.server
  597. if credits < 0:
  598. credits = 0
  599. self.settings[server.id]["REGISTER_CREDITS"] = credits
  600. await self.bot.say("Registering an account will now give {} credits."
  601. "".format(credits))
  602. dataIO.save_json(self.file_path, self.settings)
  603. # What would I ever do without stackoverflow?
  604. def display_time(self, seconds, granularity=2):
  605. intervals = ( # Source: http://stackoverflow.com/a/24542445
  606. ('weeks', 604800), # 60 * 60 * 24 * 7
  607. ('days', 86400), # 60 * 60 * 24
  608. ('hours', 3600), # 60 * 60
  609. ('minutes', 60),
  610. ('seconds', 1),
  611. )
  612. result = []
  613. for name, count in intervals:
  614. value = seconds // count
  615. if value:
  616. seconds -= value * count
  617. if value == 1:
  618. name = name.rstrip('s')
  619. result.append("{} {}".format(value, name))
  620. return ', '.join(result[:granularity])
  621. def check_folders():
  622. if not os.path.exists("data/economy"):
  623. print("Creating data/economy folder...")
  624. os.makedirs("data/economy")
  625. def check_files():
  626. f = "data/economy/settings.json"
  627. if not dataIO.is_valid_json(f):
  628. print("Creating default economy's settings.json...")
  629. dataIO.save_json(f, {})
  630. f = "data/economy/bank.json"
  631. if not dataIO.is_valid_json(f):
  632. print("Creating empty bank.json...")
  633. dataIO.save_json(f, {})
  634. def setup(bot):
  635. global logger
  636. check_folders()
  637. check_files()
  638. logger = logging.getLogger("red.economy")
  639. if logger.level == 0:
  640. # Prevents the logger from being loaded again in case of module reload
  641. logger.setLevel(logging.INFO)
  642. handler = logging.FileHandler(
  643. filename='data/economy/economy.log', encoding='utf-8', mode='a')
  644. handler.setFormatter(logging.Formatter(
  645. '%(asctime)s %(message)s', datefmt="[%d/%m/%Y %H:%M]"))
  646. logger.addHandler(handler)
  647. bot.add_cog(Economy(bot))