diff --git a/Points.py b/Points.py new file mode 100644 index 0000000..d044d8f --- /dev/null +++ b/Points.py @@ -0,0 +1,126 @@ +import asyncio +import discord +from discord.ext import commands +from pprint import pprint +import time +import numpy as np +import random + +class Points(object): + """Geef elkaar punten!""" + + def __init__(self, bot): + self.bot = bot + self.voice_states = {} + + #a = discord.get_channel(self.bot.hdChannels["shitpost"]) + #pprint(a) + + def __check(self, ctx): + return True + + @commands.command(pass_context=True, hidden=False) + @asyncio.coroutine + def geefpunten(self, ctx, user, points: int): + """Wees eens lief. Geef iemand wat punten. + Bijvoorbeeld: + !geefpunten @ikbenjp 42 + Let op: je moet iemand taggen om punten uit te delen + """ + + if points == 0: + yield from ctx.channel.send("0 punten? Gul hoor.") + return + if points < 0: + yield from ctx.channel.send("Doeslief. Je mag (nog) geen punten afnemen.") + return + + if user[0:2] != "<@": + try: + yield from ctx.message.delete() + yield from ctx.message.author.send("Je moet iemand taggen om punten te geven. :)") + #TODO ook userId oid toestaan? + except: + print("No permission to delete message or send DM") + return + + userObj = self.bot.get_user( int(user.replace("<@", "").replace(">", "")) ) + + if not self.isUserOnList(userObj): + yield from ctx.channel.send("Nee. Die krijgt geen punten.") + return + if self.getUserBalance(ctx.author) < points: + yield from ctx.channel.send("Daar heb jij niet genoeg puntjes voor. :bell:") + return + + self.transferPoints(ctx.author, userObj, points) + + message = self.getMessageForAwardingPoints(ctx.author.name, userObj.name, points) + yield from ctx.channel.send(message) + + + @commands.command(pass_context=True, hidden=False) + @asyncio.coroutine + def puntentelling(self, ctx): + """Hoeveel punten heeft iedereen? + """ + + #TODO format + #TODO sort + message = "De puntentelling: \n" + pointsCount = np.load('pointscount.npy').item() + for user in pointsCount: + message += str(pointsCount[user]["points"]) + " " + pointsCount[user]["name"] + "\n" + yield from ctx.channel.send(message) + + + # Transfer points form one user to another + def transferPoints(self, fromUser, toUser, points): + pointsCount = np.load('pointscount.npy').item() + pointsCount[fromUser.id]["points"] -= points + pointsCount[toUser.id]["points"] += points + np.save("pointscount.npy", pointsCount) + print(pointsCount) + + # Do we know this user? + def isUserOnList(self, user): + pointsCount = np.load('pointscount.npy').item() + return user.id in pointsCount.keys() + + # Get a user's point balance + def getUserBalance(self, user): + pointsCount = np.load('pointscount.npy').item() + return pointsCount[user.id]["points"] + + # Generate a semi-random text to tell somebody they got some shiny new points + def getMessageForAwardingPoints(self, userNameFrom: str, userNameTo: str, points: int): + p = str(points) + messages = [ + p + " punten voor " + userNameTo + ".", + userNameTo + " krijgt " + p + " punten.", + userNameTo + " krijgt " + p + " punten van " + userNameFrom, + p + " POINTS TO GRYFFINDO.. oh.. " + userNameTo + ".", + userNameTo + ", jij krijgt " + p + " puntjes van " + userNameFrom + ". Lief he? :heart:", + "+" + p + " punten voor " + userNameTo + "! Highscore!", + userNameTo + ", alsjeblieft, " + p + " punten.", + "Hier zijn " + p + " punten, " + userNameTo + ". Don't spend it all in one place...", + "Jij kan wel " + p + " punten gebruiken, " + userNameTo + ".", + p + " blockchain smartcontract punten voor " + userNameTo + ". :rocket: " + ] + return messages[random.randint(0, len(messages)-1)] + + + def init(self): + # Check if points file exists: + try: + pointsCount = np.load('pointscount.npy').item() + except FileNotFoundError: + np.save("pointscount.npy", {}) + pointsCount = np.load('pointscount.npy').item() + + memberList = self.bot.get_channel(self.bot.hdChannels["shitpost"]).members + for member in memberList: + # Give new members 100 points + if member.id not in pointsCount.keys() and not member.bot: + pointsCount[member.id] = {"name": member.name, "points": 100} + np.save("pointscount.npy", pointsCount) diff --git a/Stroopwafel.py b/Stroopwafel.py index 535184d..1086b15 100644 --- a/Stroopwafel.py +++ b/Stroopwafel.py @@ -5,6 +5,7 @@ import Shitpost import General import Cryptocoin import Chatbot +import Points import logging import importlib import configparser @@ -20,7 +21,9 @@ bot = commands.Bot(command_prefix=commands.when_mentioned_or('!'), description=' config = configparser.ConfigParser() config.read('stroopwafel.ini') bot.tokens = config["tokens"] -bot.hdChannels = config["channels"] +bot.hdChannels = {} +for channel in config["channels"]: #channelIDs must be integers + bot.hdChannels[channel] = int(config["channels"][channel]) #passwords are base64 encoded, which makes it like 1% safer bot.passwords = {} for pw in config["passwords"]: @@ -36,10 +39,14 @@ bot.add_cog(Shitpost.Shitpost(bot)) bot.add_cog(General.General(bot)) bot.add_cog(Cryptocoin.Cryptocoin(bot)) bot.add_cog(Chatbot.Chatbot(bot)) +pointsBot = Points.Points(bot) +bot.add_cog(pointsBot) @bot.event @asyncio.coroutine def on_ready(): print('Logged in as:\n{0} (ID: {0.id})'.format(bot.user)) + #TODO: find out how to do this properly: + pointsBot.init() bot.run(bot.tokens["discordbot"])