-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
89 lines (74 loc) · 2.52 KB
/
main.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
"""
Main script for Witness: Social Deducation Word Game
Runs the Discord bot client and handles user messages and reactions.
"""
import discord
import os
from dotenv import load_dotenv
from gameplay import Game
MAX_GAMES = 3
MAX_PLAYERS = 12
# Discord client settings
intents = discord.Intents.default()
intents.message_content = True
intents.reactions = True
client = discord.Client(intents=intents)
# List of ongoing Witness games
game_list = []
@client.event
async def on_ready():
'''
Actions in response to logging in.
'''
print('DISCORD BOT logged in as {0.user}.'.format(client))
@client.event
async def on_message(message):
'''
Actions in response to a user message.
'''
# Ignore messages sent by self
if message.author.id == client.user.id:
return
# Response to $hello with "Hello!"
if message.content == "$hello":
await message.channel.send("Hello!")
return
# Start new Witness game on $play
if message.content == "$play":
if len(game_list) < MAX_GAMES:
game_list.append(await Game.initialize(message))
else:
await message.channel.send(f"Cannot create a new game. There are already ongoing {MAX_GAMES} games.")
return
# Clean up existing Witness categories and channels on $prune
if message.content == "$prune":
for category in message.guild.categories:
if category.name.startswith("Witness-"):
for channel in category.channels:
await channel.delete()
await category.delete()
return
# Find the game associated with the message and let the Game handle the message
for game in game_list:
if message.channel.category.id == game.category.id:
await game.handle_message(message)
return
@client.event
async def on_reaction_add(reaction, user):
'''
Actions in response to a message reaction
'''
# Ignore reactions made by self
if user == client.user.id:
return
# If a user reacts to a game registration message, then add the user as a player in that game
for game in game_list:
if reaction.message.id == game.registration_msg.id:
if user not in [player.user for player in game.player_list]:
if len(game.player_list) < MAX_PLAYERS:
await game.add_player(user)
return
# Take environment variables from .env
load_dotenv()
# Run discord client
client.run(os.getenv('DISCORD_TOKEN'))