-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModuleGiveAwayMachine.py
192 lines (156 loc) · 6.75 KB
/
ModuleGiveAwayMachine.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
from Module import Module
from EventHandler import EventHandler
from twitchAPI.chat import ChatCommand
from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi_htmx import htmx, htmx_init
from twitchAPI.object.api import ChannelFollowersResult
import random
class entrygiveaway:
def __init__(self, name, pid, isusersub, isfollower, pfp):
self.name = name
self.id = pid
self.isusersub = isusersub
self.isfollower = isfollower
self.pfp = pfp
entrylist : list[entrygiveaway] = []
entrycount = 0
currentgiveawaycommand = ""
winner: list[entrygiveaway] = []
giveawayrunning = "False"
error = ""
togglepause = "False"
router = APIRouter()
htmx_init(templates = Jinja2Templates(directory="templates"))
def construct_entrylist():
numberofentries = len(entrylist)
haswinner = "False"
if len(winner) != 0:
haswinner = "True"
if numberofentries == 0:
return {"currentgiveawaycommand": currentgiveawaycommand,
"unqiueentrycount": "None",
"numberofentries": "NONE",
"giveawayrunning": giveawayrunning,
"haswinner": haswinner,
"winner": winner,
"togglepause": togglepause,
"error": error}
return {"currentgiveawaycommand": currentgiveawaycommand,
"unqiueentrycount": entrycount,
"numberofentries": numberofentries,
"giveawayrunning": giveawayrunning,
"haswinner": haswinner,
"winner": winner,
"togglepause": togglepause,
"error": error}
@router.get("/giveaway", response_class=HTMLResponse)
@htmx("giveaway","index", construct_entrylist)
def get_entrylist(request: Request):
pass
class giveAwayMachine(Module):
def __init__(self, eventHandler):
super().__init__("give_away_machine", eventHandler)
self.event_Handler.loginfo(self.name, " module loaded")
async def add_entry(self, cmd: ChatCommand):
if togglepause == "True":
return
self.event_Handler.loginfo(self.name, f"{cmd.user.name} entered the giveaway")
global entrylist, entrycount
username = cmd.user.name
userid = cmd.user.id
issub = cmd.user.subscriber
isfollower = ""
followertest: ChannelFollowersResult = await self.event_Handler.TwitchAPI.get_if_follower(username)
if len(followertest.data) > 0:
isfollower = True
else:
isfollower = False
userpfp = await self.event_Handler.get_pfp(username)
playerentry = entrygiveaway(username, userid, issub, isfollower, userpfp)
#check if user is in list
for entry in entrylist:
if entry.id == userid:
await self.event_Handler.send_message(f"{username} is already in the list!")
return
for entry in winner:
if entry.id == userid:
return
#check if user is sub (subs get x2)
if cmd.user.subscriber == True:
#add user to list twice
entrylist.append(playerentry)
entrylist.append(playerentry)
entrycount += 1
await self.event_Handler.send_message(f"{username} entered the giveaway!")
async def startgiveaway(self, command):
self.event_Handler.TwitchAPI.CHAT.register_command(command, self.add_entry)
global currentgiveawaycommand, giveawayrunning, entrylist, entrycount, winner
entrylist.clear()
winner.clear()
entrycount = 0
giveawayrunning = "True"
currentgiveawaycommand = command
self.event_Handler.loginfo(self.name, f"giveaway started with command !{command}")
self.event_Handler.eventtofrontend(self.name, f"giveaway started with command !{command}")
await self.event_Handler.send_message(f"Giveaway started! Type !{command} to enter!")
async def endgiveaway(self):
global currentgiveawaycommand, giveawayrunning, entrylist, entrycount, winner
entrylist.clear()
winner.clear()
entrycount = 0
giveawayrunning = "False"
self.event_Handler.TwitchAPI.CHAT.unregister_command(currentgiveawaycommand)
self.event_Handler.loginfo(self.name, f"giveaway ended")
self.event_Handler.eventtofrontend(self.name, f"giveaway ended")
await self.event_Handler.send_message(f"Giveaway ended!")
currentgiveawaycommand = ""
#pick winner
async def pickwinner(self):
global entrylist, winner, entrycount, error
if len(entrylist) == 0:
error = "Error: no entries!"
return
else:
error = ""
#shuffle 3 times
random.shuffle(entrylist)
random.shuffle(entrylist)
random.shuffle(entrylist)
winnerthisround = random.choice(entrylist)
for per in winner:
if per.id == winnerthisround.id:
error = "Error: this Winner was already picked!"
return
winner.append(winnerthisround)
error = ""
entrycount = entrycount - 1
entrylist = [item for item in entrylist if item.id != winnerthisround.id]
#await self.event_Handler.send_message(f"The winner is {winnerthisround.name}!")
#entrylist.clear()
self.event_Handler.loginfo(self.name, f"winner picked and announced {winnerthisround.name}")
self.event_Handler.eventtofrontend(self.name, f"winner picked and announced {winnerthisround.name}")
async def on_webfrontend_message(self, command):
global winner, togglepause
if type(command) == dict:
if list(command.keys())[0] == "start_giveaway":
await self.startgiveaway(command["start_giveaway"])
if list(command.keys())[0] == "end_giveaway":
await self.endgiveaway()
if list(command.keys())[0] == "pick_winner":
await self.pickwinner()
if list(command.keys())[0] == "toggle_pause":
global togglepause
if togglepause == "False":
togglepause = "True"
else:
togglepause = "False"
if list(command.keys())[0] == "winner_announce":
personid = command["winner_announce"]
for person in winner:
if person.id == personid:
await self.event_Handler.send_message(f"The winner is @{person.name}!")
if list(command.keys())[0] == "winner_remove":
personid = command["winner_remove"]
winner = [item for item in winner if item.id != personid]