-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpyOnServer.py
255 lines (203 loc) · 8.41 KB
/
SpyOnServer.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
import discord
from discord.ext import commands
import random
# game words
words = ['Physics' , 'Maths' , 'Chemistry']
# a dictionary of games
games = {}
#creates the client
prefix = '/'
client = commands.Bot(command_prefix= prefix)
# player. not users! user are linked to players in the Game object.
class player:
def __init__(self, name):
self.name = name
self.wins = 0.0
self.spy = False
self.suspicions = 0
self.vote = False
self.knows = True
#adds to players wins for scoreboard
def wins(self):
self.wins += 1
#reinitiates player spy status
def replay(self):
self.spy = False
#sets the player as the spy
def setSpy(self):
self.spy = True
self.knows = False
#Game object. all the game properties are in stored in this object.
class Game:
def __init__(self):
self.GameStarted = False
self.players = {}
self.gameMessage = None
self.startMessage = None
self.channel = None
self.votes = {}
self.spy = None
self.word = None
self.answerMessage = None
#adds a player to the players list.
async def addPlayer(self, member):
self.players[member] = player(member.name)
# print(f'player {member.name} is added')
# removes a player from players list.
async def removePlayer(self , member):
self.players.pop(member)
# print(f'player {member.name} is removed')
#assign the spy by random from players list.
async def assignSpy(self):
spyMember = random.choice(list(self.players.keys()))
self.spy = self.players[spyMember]
self.spy.setSpy()
# finds and return the most suspicious player.
def mostVoted(self):
voted = player('sb')
for culprit in self.players.values():
if culprit.suspicions > voted.suspicions:
voted = culprit
return voted
#sends all users DMs. the spy will recieve "you are the SPY" and the rest will recieve the game word
async def sendUserMessages(self):
for user in self.players.keys():
if self.players[user] is self.spy:
await user.send('You are the SPY!')
else:
await user.send('the word is ' + self.word)
#re-initiates the game object, sends the gameMessage and start the game.
async def start(self):
await self.channel.send('Beep! Beep! Beep!')
self.word = random.choice(words)
# checks if the game has begun and send relative message
if self.GameStarted == False:
self.GameStarted = True
self.gameMessage = await self.channel.send('To All Agents! \nATTENTION! \nThere is a RAT among us! Find the culprit and bring him in ASAP!')
await self.assignSpy()
await self.sendUserMessages()
listOfPlayers = 'Agents: \n'
n = 0
character= ['0️⃣', '1️⃣' , '2️⃣' , '3️⃣' , '4️⃣', '5️⃣' , '6️⃣' , '7️⃣' , '8️⃣' , '9️⃣', '🔟' ]
for player in self.players.values():
listOfPlayers += f'{n} : {player.name} \n'
reaction = await self.gameMessage.add_reaction(character[n])
self.votes[character[n]] = player
n += 1
await self.channel.send(listOfPlayers)
else:
await set.channel.send('The search has already begun! Continue to find the Spy!')
#resets the game properties.
def reset(self):
self.spy = None
self.GameStarted = False
#finishes the game and reloads the game object.
async def finishGame(self):
if self.spy.knows:
await self.channel.send(f'You have FAILED, Agents! The spy {self.spy.name} has found our secret!! ')
else:
if self.spy is self.mostVoted() :
for player in self.players.values():
if not player.spy:
await self.channel.send(f'The Spy is {self.spy.name}! Congratulations, Agent {player.name}! You are rewarded +0.5pts and the spy{self.spy.name} will lose 0.5 pts!')
player.wins += 0.5
else:
player.wins -= 0.5
else :
await self.channel.send(f'You have FAILED, Agent! The spy has won this time! {self.spy.name} was the culprit!')
self.spy.wins += 1.0
self.reset()
# resends the startMessage
async def callStartMessage(game):
channel = game.channel
game.startMessage = await channel.send('Are you ready, Agent?!')
await game.startMessage.add_reaction('➕')
#This event makes sure that the bot is online
@client.event
async def on_ready():
print('Bot is online!')
#this events triggers by adding reactions and handles the game joining and adding votes.
@client.event
async def on_raw_reaction_add(payload):
guild = await client.fetch_guild(payload.guild_id)
game = games[guild]
channel = await client.fetch_channel(payload.channel_id)
message = await channel.fetch_message(payload.message_id)
member = await client.fetch_user(payload.user_id)
reaction = payload.emoji
# print('reaction added')
if message.id == game.startMessage.id :
if not game.GameStarted :
if not member.bot:
await game.addPlayer(member)
# else:
# print('this player is a bot!!!!')
else :
await channel.send('The Game has already started!')
await message.remove_reaction(reaction , member)
elif message.id == game.gameMessage.id :
if not member.bot and not game.players[member].vote :
game.votes[reaction.name].suspicions += 1
game.players[member].vote = True
elif not member.bot and game.players[member].vote :
for reactionCnt in message.reactions :
if reactionCnt.emoji != reaction.name:
await message.remove_reaction(reactionCnt , member)
elif message.id == game.gameMessage.id :
if not member.bot:
await game.finishGame()
#this events triggers by removing reactions and handles the game leaving and removing votes.
@client.event
async def on_raw_reaction_remove(payload):
guild = await client.fetch_guild(payload.guild_id)
game = games[guild]
channel = await client.fetch_channel(payload.channel_id)
message = await channel.fetch_message(payload.message_id)
member = await client.fetch_user(payload.user_id)
reaction = payload.emoji
# print('reaction removed')
if message.id == game.startMessage.id :
if not game.GameStarted:
if not member.bot:
await game.removePlayer(member)
# else:
# print('this player is a bot!!!!')
elif message.id == game.gameMessage.id :
game.votes[reaction.name].suspicions -= 1
game.players[member].vote = False
# makes the bot ready
@client.command()
async def SpyOnServer(ctx):
guild = ctx.message.guild
game = Game()
games[guild] = game
game.channel = await guild.create_text_channel('Top Secret Channel')
everyoneRole = guild.get_role(guild.id)
await game.channel.set_permissions(everyoneRole , read_messages = True , send_messages = False)
await callStartMessage(game)
#Starts the Game
@client.command()
async def GameStart(ctx):
guild = ctx.message.guild
game = games[guild]
await game.start()
#this command finishes and resets the game
@client.command()
async def finishGame(ctx):
guild = ctx.message.guild
game = games[guild]
await game.finishGame()
# this command gets the spy's answer and adds the verification reaction
@client.command()
async def Answer(ctx):
guild = ctx.message.guild
game = games[guild]
if ctx.message.author == game.spy:
game.answerMessage = await ctx.message.channel.send(f'the spy has revealed himself! we need to verify if he knows our secret! Is {ctx.message.content} the secret?!')
await game.answerMessage.add_reaction('➕')
else:
await ctx.message.delete()
await ctx.message.channel.send('You Are NOT the Spy, You IDIOT!!!!')
# takes the token.txt containing the discord app token
token = open("token.txt" , "r")
client.run(token.readline())#recieves the token generated by discord as string. My token is regenarate! :D