Completed
Push — stable ( ce9d65...6568cb )
by Sydney
02:01 queued 01:19
created

prefix_set()   A

Complexity

Conditions 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 17
c 2
b 0
f 0
rs 9.4285
cc 3
1
#    Copyright 2017 Starbot Discord Project
2
#
3
#    Licensed under the Apache License, Version 2.0 (the "License");
4
#    you may not use this file except in compliance with the License.
5
#    You may obtain a copy of the License at
6
#
7
#        http://www.apache.org/licenses/LICENSE-2.0
8
#
9
#    Unless required by applicable law or agreed to in writing, software
10
#    distributed under the License is distributed on an "AS IS" BASIS,
11
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
#    See the License for the specific language governing permissions and
13
#    limitations under the License.
14
15
'''Functions for commonly used database items'''
16
from api import database
17
from api.database.table import Table, TableTypes
18
# Manage stored prefixes in database.
19
20
21
def prefix_set(id_server, prefix):
22
    '''Set a server\'s prefix.'''
23
    database.init()
24
    table_prefix = Table('prefixes', TableTypes.pGlobal)
25
26
    try:
27
        entry_prefix = Table.search(table_prefix, 'serverid', '{}'.format(id_server))
28
    except:
29
        # TODO: Narrow this and other Exception clauses.
30
        # Table must be empty.
31
        entry_prefix = None
32
33
    if entry_prefix:
34
        entry_prefix.edit(dict(serverid=id_server, prefix=prefix))
35
    else:
36
        # Create new entry
37
        Table.insert(table_prefix, dict(serverid=id_server, prefix=prefix))
38
39
40
def prefix_get(id_server):
41
    '''Get a server\'s prefix.'''
42
    database.init()
43
    table_prefix = Table('prefixes', TableTypes.pGlobal)
44
45
    try:
46
        entry_prefix = Table.search(table_prefix, 'serverid', '{}'.format(id_server))
47
    except:
48
        # TODO: Narrow this and other Exception clauses.
49
        # Table must be empty.
50
        entry_prefix = None
51
52
    if entry_prefix:
53
        return entry_prefix.data[1]
54
    else:
55
        return '!'
56
57
58
# Manage bot ownership.
59
60
61
def owners_check(id_user):
62
    '''Check if a user is an owner'''
63
    database.init()
64
    table_owners = Table('owners', TableTypes.pGlobal)
65
    try:
66
        entry_owner = Table.search(table_owners, 'userid', '{}'.format(id_user))
67
    except:
68
        # TODO: Narrow this and other Exception clauses.
69
        # Table must be empty.
70
        entry_owner = None
71
72
    if entry_owner:
73
        return True
74
    else:
75
        return False
76
77
# TODO: Make tables iterable.
78
79
# def owners_get():
80
#     database.init()
81
#     table_owners = table('owners', tableTypes.pGlobal)
82
#     owners = []
83
#     for entry in table_owners:
84
#         owners.append(entry.data[0])
85
#     return owners
86
87
88
def owners_get():
89
    '''Return an array of owner IDs from the database'''
90
    import sqlite3
91
    conn = sqlite3.connect("bot.db3")
92
    cursor = conn.cursor()
93
    cursor.execute('CREATE TABLE IF NOT EXISTS owners (userid INTEGER)')
94
    cur = cursor.execute('SELECT userid FROM owners')
95
    owners = []
96
    for row in list(cur):
97
        owners.append(row[0])
98
    conn.commit()
99
    conn.close()
100
    return owners
101
102
103
def owners_add(id_user):
104
    '''Add an owner to the database'''
105
    database.init()
106
    table_owners = Table('owners', TableTypes.pGlobal)
107
    try:
108
        entry_owner = Table.search(table_owners, 'userid', '{}'.format(id_user))
109
    except:
110
        # TODO: Narrow this and other Exception clauses.
111
        # Table must be empty.
112
        entry_owner = None
113
114
    if not entry_owner:
115
        Table.insert(table_owners, dict(userid=id_user))
116