· 7 years ago · Sep 26, 2018, 05:44 PM
1import discord
2import os
3from discord.ext.commands import Bot
4from utils.dataIO import dataIO
5import sqlite3 as db
6
7conn = db.connect("fashion.db")
8c = conn.cursor()
9
10
11# TRYING TO DECIDE HOW I WANT THE DB INFRASTRUCTURE. LEAVE COMMENTED FOR NOW.
12"""Create the table for tracking channels and tags"""
13# c.execute("CREATE TABLE IF NOT EXISTS channels (channel text, tag text)")
14
15"""Create the tables for outfit storage"""
16# c.execute("CREATE TABLE IF NOT EXISTS male (id text, link text, submitter text)")
17# c.execute("CREATE TABLE IF NOT EXISTS female (id text, link text, submitter text)")
18# c.execute("CREATE TABLE IF NOT EXISTS disaster (id text, link text, submitter text)")
19# c.execute("CREATE TABLE IF NOT EXISTS osrs (id text, link text, submitter text)")
20# c.execute("CREATE TABLE IF NOT EXISTS collab (id text, link text, submitter text)")
21
22
23def check_folder():
24 if not os.path.exists('data'):
25 try:
26 os.mkdir('data')
27 print('Creating data folder...')
28 except FileExistsError:
29 pass
30
31
32def check_file():
33 data = {}
34 a = "data/settings.json"
35 b = "data/cogs.json"
36 if not dataIO.is_valid_json(a):
37 print("Creating data/settings.json")
38 dataIO.save_json(a, data)
39 if not dataIO.is_valid_json(b):
40 print("Creating data/cogs.json")
41 dataIO.save_json(b, '[]')
42
43
44
45async def get_pre(bot, message):
46 server = message.server
47 if server.id in settings:
48 if 'Prefix' in settings[server.id]:
49 PREFIX = settings[server.id]['Prefix']
50 else:
51 PREFIX = settings['Prefix']
52 return PREFIX
53
54
55def prefix(message):
56 server = message.server
57 if server.id in settings:
58 if 'Prefix' in settings[server.id]:
59 Prefix = [settings[server.id]['Prefix']]
60 Prefix = ' '.join(Prefix)
61 else:
62 Prefix = settings['Prefix']
63 return Prefix
64check_folder()
65check_file()
66
67settings = dataIO.load_json('data/settings.json')
68try:
69 token = settings['Token']
70except KeyError:
71 token = input("Type the bot token and press 'Enter'.")
72 settings.update({'Token' : token})
73 dataIO.save_json('data/settings.json', settings)
74if 'Prefix' not in settings:
75 PREFIX = input ("Type the default bot prefix and press 'Enter'.")
76 settings.update ({'Prefix': PREFIX})
77 dataIO.save_json ('data/settings.json', settings)
78cogsjson = dataIO.load_json ('data/cogs.json')
79startup_extensions = [cogsjson]
80bot = Bot(command_prefix=get_pre)
81
82bot.load_extension ('owner')
83owner_cog = bot.get_cog ('owner')
84if owner_cog is None:
85 print ("The owner cog is missing. It contains core functions without "
86 "which the bot cannot function. Reinstall.")
87 exit (1)
88
89@bot.event
90async def on_message(message):
91 # we do not want the bot to reply to itself
92 if message.author == bot.user:
93 return
94
95 if message.author == discord.User.bot:
96 return
97 if bot.user.mentioned_in(message):
98 if 'prefix' in message.content.lower():
99 await bot.send_message(message.channel, '**My prefix is:** `{}`'.format(prefix(message)))
100 await bot.process_commands(message)
101
102@bot.event
103async def on_ready():
104 print("-----------------")
105 print("WaffleMachine")
106 print("-----------------")
107 print(str(bot.user))
108 print("\nConnected to:")
109 print("{} servers".format(len(bot.servers)))
110 print("{} channels".format(len([c for c in bot.get_all_channels()])))
111 print("{} users\n".format(len(set(bot.get_all_members()))))
112 print("-----------------")
113
114 print("\nUse this url to bring your bot to a server:")
115 print('https://discordapp.com/api/oauth2/authorize?client_id=493161792076382208&permissions=0&scope=bot')
116
117
118try:
119 bot.run(token)
120except discord.errors.LoginFailure:
121 print("Please enter a valid discord bot token!")
122 exit()