<file_sep>import os
import discord
import time
import random
from discord.ext import commands
from dotenv import load_dotenv
from make_gif import make_gif

print('Loading tokens...')

load_dotenv("tokens.env")
TOKEN = os.getenv('DISCORD_TOKEN')
kill_pw = os.getenv('KILL_PW')
#GUILD = os.getenv('DISCORD_GUILD')

print('Building bot...')

names = ["Simon", "Jamie", "James", "Alex", "Sam", "Elliott", "Jacob", "Will", "Ned", "Jake", "Chappers"]
thinking = ["Hmmm let me think.", "This is an easy one.", "Now this is HLTV confirmed.", "Tricky one...", "Errrm.",
            "OK this one is close.", "Stupid question..."]


class CustomClient(discord.Client):
    async def on_ready(self):
        print("{} has connected to Discord!".format(self.user))
        for g in self.guilds:
            print("Connected to {}".format(g))


bot = commands.Bot(command_prefix='!')


@bot.command(name='kill', help='Protected kill command. Shuts off bot.')
async def kill(ctx, *, pw):
    await ctx.message.delete()
    if str(pw) == kill_pw:
        await ctx.send("Putting bot to sleep...")
        await bot.logout()
    else:
        await ctx.send("No permission to kill bot.")


@bot.command(name='fak', help='Type !fak to fuck something.')
async def fak(ctx, *, thing):
    await ctx.send("Fuck {}".format(thing))


@bot.command(name='who', help='Type !who is blank thing to decide who is a thing.')
async def who(ctx, *, thing):
    await ctx.send(random.choice(thinking))
    thing = thing.replace("?", "")
    #if thing[0:3] == "is ":
    #   thing = thing[3:]
    time.sleep(1)
    await ctx.send("{} {}.".format(random.choice(names), thing), tts=True)
    #await ctx.message.delete()


@bot.command(name='chaser', help='Who is the chaser?')
async def chaser(ctx):
    voice_channel = ctx.author.voice.channel if ctx.author.voice is not None else None
    channel = None
    if voice_channel is not None:
        channel = voice_channel.name
        vc = await voice_channel.connect()
        vc.play(discord.FFmpegPCMAudio(source='chaser_walk_on.mp3'))
        while vc.is_playing():
            time.sleep(1)
        await vc.disconnect()
    else:
        await ctx.send("{} is not in a channel.".format(str(ctx.author.name)))
    await ctx.message.delete()

''' Play error. Comment out for now.
@bot.command(name='minceintro', help='Relaxing music.')
async def minceintro(ctx):
    voice_channel = ctx.author.voice.channel if ctx.author.voice is not None else None
    channel = None
    if voice_channel is not None:
        channel = voice_channel.name
        vc = await voice_channel.connect()
        vc.play(discord.FFmpegPCMAudio(source='audio/minceintro.mp3'))
        while vc.is_playing():
            time.sleep(1)
        await vc.disconnect()
    else:
        await ctx.send("{} is not in a channel.".format(str(ctx.author.name)))
    await ctx.message.delete()
'''


@bot.command(name='goal', help='BBC WM Goal Horn')
async def goal(ctx):
    voice_channel = ctx.author.voice.channel if ctx.author.voice is not None else None
    channel = None
    if voice_channel is not None:
        channel = voice_channel.name
        vc = await voice_channel.connect()
        vc.play(discord.FFmpegPCMAudio(source='WM_Goal.mp3'))
        while vc.is_playing():
            time.sleep(1)
        await vc.disconnect()
    else:
        await ctx.send("{} is not in a channel.".format(str(ctx.author.name)))
    await ctx.message.delete()


@bot.command(name='retard', help='m0e asks a question')
async def retard(ctx):
    voice_channel = ctx.author.voice.channel if ctx.author.voice is not None else None
    channel = None
    if voice_channel is not None:
        channel = voice_channel.name
        vc = await voice_channel.connect()
        vc.play(discord.FFmpegPCMAudio(source='audio/retard_m0e.mp3'))
        while vc.is_playing():
            time.sleep(1)
        await vc.disconnect()
    else:
        await ctx.send("{} is not in a channel.".format(str(ctx.author.name)))
    await ctx.message.delete()


@bot.command(name='fuck', help='Steel finds out @tomi is still tweeting')
async def fuck(ctx):
    voice_channel = ctx.author.voice.channel if ctx.author.voice is not None else None
    channel = None
    if voice_channel is not None:
        channel = voice_channel.name
        vc = await voice_channel.connect()
        vc.play(discord.FFmpegPCMAudio(source='audio/fuck.mp3'))
        while vc.is_playing():
            time.sleep(1)
        await vc.disconnect()
    else:
        await ctx.send("{} is not in a channel.".format(str(ctx.author.name)))
    await ctx.message.delete()


@bot.command(name='shitting', help='Steel has piles?')
async def shitting(ctx):
    voice_channel = ctx.author.voice.channel if ctx.author.voice is not None else None
    channel = None
    if voice_channel is not None:
        channel = voice_channel.name
        vc = await voice_channel.connect()
        vc.play(discord.FFmpegPCMAudio(source='audio/shitting_me.mp3'))
        while vc.is_playing():
            time.sleep(1)
        await vc.disconnect()
    else:
        await ctx.send("{} is not in a channel.".format(str(ctx.author.name)))
    await ctx.message.delete()


@bot.command(name='defuse', help='For trolling only.')
async def defuse(ctx):
    voice_channel = ctx.author.voice.channel if ctx.author.voice is not None else None
    channel = None
    if voice_channel is not None:
        channel = voice_channel.name
        vc = await voice_channel.connect()
        vc.play(discord.FFmpegPCMAudio(source='audio/defuse.mp3'))
        while vc.is_playing():
            time.sleep(1)
        await vc.disconnect()
    else:
        await ctx.send("{} is not in a channel.".format(str(ctx.author.name)))
    await ctx.message.delete()


@bot.command(name='idgi', help='I don\'t get it')
async def idgi(ctx):
    channel = ctx.message.channel
    await channel.send(file=discord.File('idgi.jpg'))
    voice_channel = ctx.author.voice.channel if ctx.author.voice is not None else None
    channel = None
    if voice_channel is not None:
        channel = voice_channel.name
        vc = await voice_channel.connect()
        vc.play(discord.FFmpegPCMAudio(source='audio/idgi.mp3'))
        while vc.is_playing():
            time.sleep(1)
        await vc.disconnect()
    await ctx.message.delete()

@bot.command(name='qi', help='ALARM - !qi text')
async def qi(ctx, *, text):
    channel = ctx.message.channel
    await ctx.message.delete()
    print(text)
    await channel.send(file=discord.File(make_gif(text)))
    voice_channel = ctx.author.voice.channel if ctx.author.voice is not None else None
    channel = None
    if voice_channel is not None:
        channel = voice_channel.name
        vc = await voice_channel.connect()
        vc.play(discord.FFmpegPCMAudio(source='audio/qi.mp3'))
        while vc.is_playing():
            time.sleep(1)
        await vc.disconnect()


print('Bot starting!')
bot.run(TOKEN)

#client = CustomClient()
#client.run(TOKEN)
<file_sep># OS modules
import os
import shutil

# Image modules
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw


def make_gif(text):

    # create/delete our temp files folder
    if os.path.exists('frames'):
        shutil.rmtree('frames')
    os.mkdir('frames')

    # load in the background image
    img_background = Image.open('imgs/black_bg.png')
    img = Image.new("RGBA", img_background.size, (0, 0, 0, 255))

    # set up colours and vars
    x = 10
    y = 10
    W = 500
    H = 300

    white = (255, 255, 255, 255)


    # load the font
    font_size = max(10,65-(max(0, len(text)-48)*4))
    font_size = 65
    font = ImageFont.truetype("RobotoR.ttf", font_size)
    text = text_wrap(text, font, W-50)
    text = "\n".join(text)
    draw = ImageDraw.Draw(img)

    # add text to each frame
    for N in range(0, 8):
        img.paste(img_background, (0, 0))
        if (N % 2) == 0:
            w, h = draw.textsize(text, font=font)
            draw.multiline_text(((W - w) / 2, (H - h) / 2), text, fill="white", font=font, align="center")
            #draw.text((x, y), text, white, font=font)
        img.save("./frames/{}.png".format(str(N).zfill(3)))

    # output the result
    os.system('ffmpeg -framerate 2 -i frames/%03d.png -c:v ffv1 -r 8 -y -loop -1 out.avi')
    os.system('ffmpeg -y -i out.avi out.gif')

    # clean up
    shutil.rmtree('frames')
    return "out.gif"


def text_wrap(text, font, max_width):
    """Wrap text base on specified width.
    This is to enable text of width more than the image width to be display
    nicely.
    @params:
        text: str
            text to wrap
        font: obj
            font of the text
        max_width: int
            width to split the text with
    @return
        lines: list[str]
            list of sub-strings
    """
    lines = []

    # If the text width is smaller than the image width, then no need to split
    # just add it to the line list and return
    if font.getsize(text)[0] <= max_width:
        lines.append(text)
    else:
        # split the line by spaces to get words
        words = text.split(' ')
        i = 0
        # append every word to a line while its width is shorter than the image width
        while i < len(words):
            line = ''
            while i < len(words) and font.getsize(line + words[i])[0] <= max_width:
                line = line + words[i] + " "
                i += 1
            if not line:
                line = words[i]
                i += 1
            lines.append(line)
    return lines