Passed
Branch unstable (79b831)
by Sydney
01:37
created

api.settings   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 57
dl 0
loc 119
rs 10
c 0
b 0
f 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
A prefix_get() 0 16 3
A prefix_set() 0 17 3
A owners_get() 0 16 3
A owners_check() 0 15 3
A owners_add() 0 13 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
    try:
95
        cur = cursor.execute('SELECT userid FROM owners')
96
    except:
97
        return []
98
    owners = []
99
    for row in list(cur):
100
        owners.append(row[0])
101
    conn.commit()
102
    conn.close()
103
    return owners
104
105
106
def owners_add(id_user):
107
    '''Add an owner to the database'''
108
    database.init()
109
    table_owners = Table('owners', TableTypes.pGlobal)
110
    try:
111
        entry_owner = Table.search(table_owners, 'userid', '{}'.format(id_user))
112
    except:
113
        # TODO: Narrow this and other Exception clauses.
114
        # Table must be empty.
115
        entry_owner = None
116
117
    if not entry_owner:
118
        Table.insert(table_owners, dict(userid=id_user))
119