import random
import commands
import os

path = os.path.dirname(os.path.abspath(__file__))


@commands.on_startup()
def load_kick_msgs(forkbot):
    try:
        with open(path + "/files/kickmsgs.txt", "r") as f:
            msgs = f.read().split("\n")
    except FileNotFoundError:
        msgs = ["Kick"]
    forkbot.DB.add_db("kick_msgs")
    forkbot.DB.update_db("kick_msgs", msgs)


@commands.command("kick")
def kick(forkbot):
    if forkbot.info.is_op or forkbot.info.is_bot_admin:
        channel = forkbot.info.channel[:]
        msg = forkbot.info.msg[:]
        index = msg.find(" ")
        if index != -1 and index != len(msg)-1:
            user, reason = msg[:index], msg[index+1:]
        else:
            user, reason = msg, random.choice(forkbot.DB.get_db("kick_msgs"))
        forkbot.raw_write(f"KICK {channel} {user} :{reason}")
    else:
        forkbot.raw_write(f"KICK {channel} {forkbot.info.nick} :{random.choice(self.DB.get_db('kick_msgs'))}")


@commands.command("ban")
def ban(forkbot):
    if forkbot.info.is_op or forkbot.info.is_bot_admin:
        channel = forkbot.info.channel[:]
        forkbot.raw_write(f"MODE {channel} +b " + forkbot.info.msg)


@commands.command("invite")
def invite(forkbot):
    if forkbot.info.channel in forkbot.settings.autojoin:
        forkbot.raw_write("INVITE " + forkbot.info.msg)
