-
Notifications
You must be signed in to change notification settings - Fork 1
/
ticket_system.py
211 lines (158 loc) · 5.51 KB
/
ticket_system.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
from decouple import config
from ticket import Ticket
from ticket import create_ticket as create
from discord import client
from discord.ext import commands
from discord.ext.commands import has_permissions, MissingPermissions
from auth import authorized
import discord
#Connection
intents = discord.Intents.default()
intents.typing = False
intents.presences = False
intents.message_content = True
token = config("DISCORD_BOT_TOKEN")
# Initialize the bot with the specified intents
#bot = discord.Client(intents=intents)
bot = commands.Bot(command_prefix='!', intents=intents)
#Queue of all teams
queue = []
#Message id that the bot will constantly update for queue
msg_id = ""
#Initialize server
@bot.event
async def on_ready():
assert(len(queue) == 0)
channel = discord.utils.get(bot.get_all_channels(), name="mentoring-queue")
#Delete all msgs
await channel.purge()
#This is the message to update for queue
await channel.send("This is the msg the bot will constantly update for the queue")
#Find most recent msg (or the only msg)
history = channel.history()
async for msg in history:
global msg_id
msg_id = msg.id
break
await update_queue_in_channel(queue, channel)
#Updates queue in the mentoring-queue channel
@bot.command()
async def update_queue_in_channel(queue, channel):
#Queue
msg = ""
if len(queue) == 0:
msg = "Empty queue"
else:
msg += "Current queue: \n\n"
for team in queue:
if team.in_progress == False:
msg += str(team.name) + ": " + str(team.reason) + "\n"
#In progress
msg += "\n\n"
if len(queue) == 0:
msg += "No teams in progress"
else:
msg += "In progress: \n\n"
for team in queue:
if team.in_progress == True:
msg += str(team.name) + ": " + str(team.reason) + "\n"
message_to_change = await channel.fetch_message(msg_id)
await message_to_change.edit(content=msg)
#Print msg
@bot.command()
async def print_msg(msg, channel):
await channel.send(msg)
#dm msg
@bot.command()
async def dm(msg, author):
dm_channel = await author.create_dm()
await dm_channel.send(msg)
#Bot receives help message
@bot.event
async def on_message(message):
author = message.author
content = message.content
channel = discord.utils.get(bot.get_all_channels(), name="mentoring-queue")
#Make sure the message isn't from us
if author == bot.user:
return
#if empty msg
if content == "":
return
#Print anything
if content.startswith("!print"):
await channel.send("Hello!")
#Instructions
if content.startswith("!mentorhelp"):
msg = """
Hi! Please keep the assistance requests in the following format:
!mentors [Table #] [Programming Languages used] [Short Description]
WARNING: If you do not follow this format, we will not get to you immediately"""
await dm(msg, author)
#If help message e.g. "!mentors Help debugging"
elif content.startswith('!mentors'):
reason = content[9:]
#if already in queue, remove original request
ticket = find_ticket(queue, author)
if ticket != None:
resolve(queue, author)
#How many ppl ahead of me?
ppl_ahead_of_me = len(queue)
#Put into queue
put_into_queue(queue, author, reason)
msg = "Currently " + str(ppl_ahead_of_me) + " people ahead"
await dm(msg, author)
await update_queue_in_channel(queue, channel)
#If moving a team into progress e.g. "!p team351"
elif content.startswith("!p"):
name = content[3:]
#Set team's in_progress to True
team = find_ticket(queue, name)
if team is None:
await dm("Unknown team. Example command: '!p team9999' ", author)
return
team.in_progress = True
await update_queue_in_channel(queue, channel)
#If moving a team from progress back to queue e.g. "!q team351"
elif content.startswith("!q"):
name = content[3:]
#Set team's in_progress to True
team = find_ticket(queue, name)
if team is None:
await dm("Unknown team. Example command: '!q team9999' ", author)
return
team.in_progress = False
await update_queue_in_channel(queue, channel)
#If resolve message e.g. "!r team351"
elif content.startswith("!r"):
#ONLY AUTHORIZED CAN DO THIS
if not authorized(author):
await dm("Not authorized", author)
return
#Team to remove from queue
name = content[3:]
ticket = find_ticket(queue, name)
if ticket is None:
await dm("Unknown team. Example command: '!r team9999' ", author)
return
resolve(queue, name)
await dm('Removed ' + name, author)
await update_queue_in_channel(queue, channel)
#Find ticket by team name
def find_ticket(list, team_name):
for ticket in list:
if str(ticket.name) == str(team_name):
return ticket
#If no ticket found
return None
#Put team that needs help in queue
def put_into_queue(list, team_name, reason):
#Create new ticket
ticket = create(team_name, reason)
list.append(ticket)
#Remove team from queue after done helping
def resolve(list, team_name):
#Find ticket
ticket = find_ticket(list, team_name)
list.remove(ticket)
bot.run(token)