forked from Jomsaruj/Light-up-the-room
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LightManagingBot.py
368 lines (345 loc) · 14.7 KB
/
LightManagingBot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import os
import discord
from discord import team, message,embeds
from discord.client import Client
from discord.ext import commands
from dotenv import load_dotenv
from flask.globals import request
from pymongo import MongoClient
from datetime import datetime
room_size = 2
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
client = commands.Bot(command_prefix='$')
client.remove_command('help')
cluster = MongoClient(os.getenv('MONGO_URL'))
db = cluster.exceed_group16
collection = db.admin_user
collection_room = db.room_info
def embed_send(Title,role,Color,args : tuple,line=False):
embedVar = discord.Embed(title=Title, description=role, color=Color)
for i in range(0,len(args),2):
embedVar.add_field(name=args[i], value=args[i+1], inline=line)
return embedVar
def info_user(message):
try:
print("category : ",message.channel.name)
except:
pass
print("Author : ",message.author)
print("Author name : ",message.author.name)
print("Author ID: ",message.author.id)
print("message : " ,message.content)
print('+'*30)
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
@client.event
async def on_message(message):
if message.author == client.user:
return
info_user(message)
await client.process_commands(message)
if 'พี่Nathan' in message.content:
await message.channel.send('สวัสดีครับน้องๆExceed')
@client.command()
async def meme(message):
await message.channel.send('https://cdn.marketingoops.com/wp-content/uploads/2017/05/meme-marketing2.png')
@client.command()
async def gif(message):
await message.channel.send('https://compote.slate.com/images/697b023b-64a5-49a0-8059-27b963453fb1.gif')
@client.command()
async def banana(message):
await message.channel.send('https://annemurray99.files.wordpress.com/2015/03/huge-dancing-banana-2.gif')
@client.command()
async def add_user(message):
user = {}
user['author_name'] = message.author.name + '#' + message.author.discriminator
user['name'] = message.author.name
user['author_ID'] = message.author.id
user['permission'] = 0
print(user)
try:
filt = {'author_name':user['author_name']}
already_in = collection.find(filt)
for ele in already_in:
return await message.channel.send("Your user has been added!!")
collection.insert_one(user)
await message.channel.send("Now, you're user.")
except:
await message.channel.send("Error!!")
@client.command()
async def promote(message,*args):
if len(args) == 0 :
return await message.channel.send("Error!!")
for arg in args:
filt2 = {'name':arg}
filt = {'author_name': message.author.name + '#' + message.author.discriminator }
admin = collection.find_one(filt)
if admin["permission"] == 1 or admin["permission"] == 2:
set_per = collection.find_one(filt2)
print(set_per)
if set_per["permission"] != 0:
return await message.channel.send(f"{arg} is already promoted.")
updated_content = {"$set": {'permission' : 1}}
collection.update_one(filt2, updated_content)
await message.channel.send(f"{arg} is promoted.")
else :
await message.channel.send("You don't have the permission.")
@client.command()
async def demote(message,*args):
if len(args) == 0 :
return await message.channel.send("Error!!")
for arg in args:
filt2 = {'name':arg}
filt = {'author_name': message.author.name + '#' + message.author.discriminator }
admin = collection.find_one(filt)
if admin["permission"] == 2:
updated_content = {"$set": {'permission' : 0}}
collection.update_one(filt2, updated_content)
await message.channel.send(f"{arg} is demoted.")
else :
await message.channel.send("You don't have the permission.")
@client.command()
async def role(message,*args):
if len(args) == 0:
filt = {'author_name': message.author.name + '#' + message.author.discriminator }
admin = collection.find_one(filt)
if admin["permission"] == 2:
await message.channel.send("You are Super Admin.")
elif admin["permission"] == 1:
await message.channel.send("You are Admin.")
else :
await message.channel.send("You are User.")
else :
for arg in args:
filt = {'name': arg}
admin = collection.find_one(filt)
try:
if admin["permission"] == 2:
await message.channel.send(f"{admin['name']} is Super Admin.")
elif admin["permission"] == 1:
await message.channel.send(f"{admin['name']} is Admin.")
else :
await message.channel.send(f"{admin['name']} is User.")
except :
await message.channel.send(f"Who is {arg} ?")
@client.command()
async def color(message,room,*args):
if len(args) == 0 :
await message.channel.send("Error syntax please try...")
await message.channel.send("$color [room] [color1] [color2] ...")
elif len(args) > 4:
await message.channel.send("Error syntax. There is only 4 colors. ")
author = collection.find_one({'author_name': message.author.name + '#' + message.author.discriminator })
if author["permission"] >=1 :
filt = {'ID':int(room),'Time_out' : None }
else:
filt = {'ID':int(room),'Time_out' : None ,'Discord' : message.author.name + '#' + message.author.discriminator}
auth = collection_room.find_one(filt)
print(auth)
try:
print(auth["Discord"])
except:
if author["permission"] >=1:
return await message.channel.send("That is empty room.")
return await message.channel.send("That isn't your room.")
updated_content = [0,0,0,0]
str_color = ''
for ele in args:
if str(ele).lower() == 'red':
updated_content[0]= 1
str_color += 'red '
elif str(ele).lower() == 'green':
updated_content[1] = 1
str_color += 'green '
elif str(ele).lower() == 'blue':
updated_content[2] = 1
str_color += 'blue '
elif str(ele).lower() == 'white':
updated_content[3] = 1
str_color += 'white '
print(updated_content)
collection_room.update_one(filt,{'$set' : {'r':updated_content[0]}})
collection_room.update_one(filt,{'$set' : {'g':updated_content[1]}})
collection_room.update_one(filt,{'$set' : {'b':updated_content[2]}})
collection_room.update_one(filt,{'$set' : {'w':updated_content[3]}})
command = ('change color to...',str_color)
print(command)
embedVar = embed_send(f"Room {room} 💡",None,0xFFC2E2,command,line=True)
await message.channel.send(embed=embedVar)
@client.command()
async def login(message,arg):
filt = {'ID':int(arg),'Time_out' : None ,'Discord' : None}
author = collection_room.find_one(filt)
print (author)
try:
print(author['ID'])
updated_content = {'$set' :{'Discord' : message.author.name + '#' + message.author.discriminator}}
collection_room.update_one(filt,updated_content)
await message.channel.send('You are now login.')
except:
await message.channel.send("You can't login. Please check room status.")
@client.command()
async def logout(message,arg):
author = collection.find_one({'author_name': message.author.name + '#' + message.author.discriminator })
print( author)
if author["permission"] >=1 :
filt = {'ID':int(arg),'Time_out' : None }
else:
filt = {'ID':int(arg),'Time_out' : None ,'Discord' : message.author.name + '#' + message.author.discriminator}
# auth = collection_room.find_one(filt)
# print(auth)
try:
info = collection_room.find_one(filt)
print(info)
updated_content = {'$set' :{'Discord' : None}}
collection_room.update_one(filt,updated_content)
if author["permission"] <1 :
print(info['Discord'])
await message.channel.send('You are now logout.')
else :
await message.channel.send('Set logout successfully.')
except:
await message.channel.send("You don't have permission to set that room information. That isn't your room.")
@client.command()
async def help(message):
filt = {'author_name': message.author.name + '#' + message.author.discriminator }
author = collection.find_one(filt)
try:
print (author["permission"])
except:
user = {}
user['author_name'] = message.author.name + '#' + message.author.discriminator
user['name'] = message.author.name
user['author_ID'] = message.author.id
user['permission'] = 0
collection.insert_one(user)
command = ('$add_user','Add you to be a new user.')
command += ('$role','Show your role.')
command += ('$login','Login to your room by\n$login [room]')
command += ('$logout','Logout from your room by\n$logout')
if author["permission"] == 0:
command += ('$status',"Show all of the room's status by \n$status or $status [room1] [room2] ..")
command += ('$color','Change color in your room by \n$color [room] [color1] [color2] ...')
command += ('$turn_on','Turn on switch in your room by \n$turn_on [room] [color1] [color2] ...')
command += ('$turn_off','Turn off switch in your room by \n$turn_off [room]')
else:
command += ('$promote','Change user to be admin by \n$add_permission [user1] ...')
if author["permission"] == 2:
command += ('$demote','Change admin to be user by \n$add_permission [user1] ...')
command += ('$status',"Show all of the room's status by \n$status or $status [room1] [room2] ..")
command += ('$color','Change color in any room by \n$color [room] [color1] [color2] ...')
command += ('$turn_on','Turn on switch in your room by \n$turn_on [room] [color1] [color2] ...')
command += ('$turn_off','Turn off switch in any room by \n$turn_off [room]')
if author["permission"] == 2:
role = f"These are Super Admin commands."
elif author["permission"] == 1:
role = f"These are Admin commands."
else :
role = f"These are User commands."
embedVar = embed_send("What I can help you? ⚙️",role,0x95FFF7,command)
await message.channel.send(embed=embedVar)
@client.command()
async def status(message,*args):
if len(args) == 0:
filt = {'Time_out':None}
room = collection_room.find(filt)
room_num = []
for i in range(room_size+1):
room_num.append('Room '+str(i))
color_room = ['empty room']*(room_size+1)
for ele in room:
print(ele)
if ele["ID"] > room_size:
continue
color_room[ele["ID"]] = 'red '*ele["r"]+'green '*ele["g"]+'blue '*ele["b"]+'white '*ele["w"]
if len(color_room[ele["ID"]]) == 0:
color_room[ele["ID"]] = 'no light'
color_room[ele["ID"]] = "light color : " + color_room[ele["ID"]]
status_room = (room_num[1],color_room[1],room_num[2],color_room[2])
embedVar = embed_send(f"Status 💡",None,0xFFC2E2,status_room,line=True)
await message.channel.send(embed=embedVar)
else:
status_room = tuple()
for room in args:
filt = {'Time_out':None,'ID':int(room)}
ele = collection_room.find_one(filt)
print (ele)
print (room)
if ele == None :
status_room += ('Room ' + str(room)),
status_room += 'empty room',
continue
color_room = 'red '*ele["r"]+'green '*ele["g"]+'blue '*ele["b"]+'white '*ele["w"]
if len(color_room) == 0:
color_room = 'no light'
color_room = "light color : " + color_room
status_room += ('Room ' + str(room)),
status_room += color_room,
embedVar = embed_send(f"Status"+" "*len(args)+"💡",None,0xFFC2E2,status_room,line=True)
await message.channel.send(embed=embedVar)
@client.command()
async def turn_on(message,room,*args):
filt = {'ID':int(room),'Time_out' : None}
auth = collection_room.find_one(filt)
if auth != None :
return await message.channel.send("This room is full.")
myInsert = {
'Type' : 'Room_info',
'ID' : int(room),
'r': 0,
'g': 0,
'b': 0,
'w': 0,
'Status' : 1,
'Time_in' : datetime.now(),
'Time_out' : None,
'Discord' : message.author.name + '#' + message.author.discriminator
}
str_color = ''
for ele in args:
if str(ele).lower() == 'red':
myInsert['r']= 1
str_color += 'red '
elif str(ele).lower() == 'green':
myInsert['g'] = 1
str_color += 'green '
elif str(ele).lower() == 'blue':
myInsert['b'] = 1
str_color += 'blue '
elif str(ele).lower() == 'white':
myInsert['w'] = 1
str_color += 'white '
collection_room.insert_one(myInsert)
if len(str_color) == 0:
str_color = 'no light'
command = ('light color : ',str_color)
print(command)
embedVar = embed_send(f"Turn on : Room {room} 💡",None,0xFFC2E2,command,line=True)
await message.channel.send(embed=embedVar)
@client.command()
async def turn_off(message,room):
author = collection.find_one({'author_name': message.author.name + '#' + message.author.discriminator })
if author["permission"] >=1 :
filt = {'ID':int(room),'Time_out' : None }
else:
filt = {'ID':int(room),'Time_out' : None ,'Discord' : message.author.name + '#' + message.author.discriminator}
auth = collection_room.find_one(filt)
print(auth)
try:
print(auth["Discord"])
except:
if author["permission"] >=1:
return await message.channel.send("That is an empty room.")
return await message.channel.send("That isn't your room.")
collection_room.update_one(filt,{'$set' : {'Status':0}})
collection_room.update_one(filt,{'$set' : {'Time_out':datetime.now()}})
command = ('change status to...','empty room')
command += ('Time in',auth['Time_in'])
command += ('Time out',str(datetime.now()))
print(command)
embedVar = embed_send(f"Turn off : Room {room} 💡",None,0xFFC2E2,command)
await message.channel.send(embed=embedVar)
if __name__ == '__main__':
client.run(TOKEN)