-
Notifications
You must be signed in to change notification settings - Fork 0
/
bugbot.py
executable file
·340 lines (255 loc) · 12.5 KB
/
bugbot.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
import asyncio
import chatproc
import common
import discord
import gpt
import logging
import os
import re
import state
import storage
import time
from discord import app_commands
from dotenv import load_dotenv
from github import Auth
from github import Github
from github import GithubIntegration
from storage import Config
from typing import Optional
# Logging
DIR_LOGS = "logs"
LOG_PATH = f"{DIR_LOGS}/{time.strftime('%Y%m%d-%H%M%S')}.log"
os.makedirs(DIR_LOGS, exist_ok = True)
log_file_handler = logging.FileHandler(LOG_PATH, mode = "w", encoding = "utf-8")
log_file_handler.setLevel(logging.DEBUG)
log_file_handler.setFormatter(logging.Formatter(common.LOG_FORMAT))
log_console_handler = logging.StreamHandler()
log_console_handler.setLevel(logging.INFO)
log_console_handler.setFormatter(logging.Formatter(common.LOG_FORMAT))
_logger = logging.getLogger()
_logger.setLevel(logging.DEBUG)
_logger.addHandler(log_file_handler)
_logger.addHandler(log_console_handler)
load_dotenv()
gpt.init(os.getenv('OPENAI_API_KEY'))
storage.init(os.getenv('MONGO_URI'))
class MyClient(discord.Client):
def __init__(self, *, intents: discord.Intents):
super().__init__(intents=intents)
self.tree = app_commands.CommandTree(self)
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
client = MyClient(intents=intents)
async def gh_open(guild_id: int = 0, repo: str = "") -> Github:
gh_key = ""
with open(f"certs/{os.getenv('GITHUB_APP_KEY')}", "r") as file:
gh_key = file.read()
gh_auth = Auth.AppAuth(os.getenv('GITHUB_APP_ID'), gh_key)
gh_integration = GithubIntegration(auth=gh_auth)
if guild_id:
config = await state.get_config(guild_id)
repo = config.github_repo.split("/")
else:
repo = repo.split("/")
gh_installation = gh_integration.get_repo_installation(repo[0], repo[1])
return gh_installation.get_github_for_installation()
async def file_issue(guild_id: int, issue_title: str, issue_md: str) -> str:
config = await state.get_config(guild_id)
gh = await gh_open(guild_id=guild_id)
repo = gh.get_repo(config.github_repo)
new_issue = repo.create_issue(title=issue_title, body=issue_md, labels=["bug"])
gh.close()
return new_issue.html_url
async def verify_config(guild_id: int, config: Config = None) -> str:
if config.github_repo == "":
return "github_repo"
try:
gh = await gh_open(repo=config.github_repo)
except Exception as e:
_logger.error(f"Error opening GitHub: {e}")
return "github_repo"
repo_name = gh.get_repo(config.github_repo).full_name
if repo_name != config.github_repo:
return "github_repo"
if config.product_name == "":
return "product_name"
if config.product_type == "":
return "product_type"
if not config.issue_categories:
return "issue_categories"
if config.discord_developer_role == "":
return "discord_developer_role"
return None
async def check_config(interaction: discord.Interaction, config: Config) -> bool:
config_error = await verify_config(interaction.guild.id, config)
if config_error:
await interaction.response.send_message(f"Configuration error: {config.get_pretty_name(config_error)}",
ephemeral=True)
return False
return True
class Setup(discord.ui.Modal, title="Setup"):
def __init__(self) -> None:
super().__init__()
self.submitted = False
self.config = None
self.repo = None
self.product = None
self.issue_categories = None
self.issue_extra_info = None
self.developer_role = None
async def populate(self, guild_id: int) -> None:
self.config = state.get_config(guild_id)
self.repo = discord.ui.TextInput(label=self.config.get_pretty_name("github_repo"),
style=discord.TextStyle.short,
placeholder="user/repo",
required=True)
self.product = discord.ui.TextInput(label=self.config.get_pretty_name("product_name") + " and " +
self.config.get_pretty_name("product_type"),
placeholder="Product Name\nProduct Type",
style=discord.TextStyle.long,
required=True)
self.issue_categories = discord.ui.TextInput(label=self.config.get_pretty_name("issue_categories"),
placeholder="Category 1\nCategory 2\nCategory 3\n...",
style=discord.TextStyle.paragraph,
required=True)
self.issue_extra_info = discord.ui.TextInput(label=self.config.get_pretty_name("issue_extra_info"),
placeholder="Info to collect 1\nInfo to collect 2\n...",
style=discord.TextStyle.paragraph,
required=True)
self.developer_role = discord.ui.TextInput(label=self.config.get_pretty_name("discord_developer_role"),
style=discord.TextStyle.short,
required=True)
self.repo.default = self.config.github_repo
self.product.default = self.config.product_name + "\n" + self.config.product_type
self.issue_categories.default = "\n".join(self.config.issue_categories)
self.issue_extra_info.default = "\n".join(self.config.issue_extra_info)
self.add_item(self.repo)
self.add_item(self.product)
self.add_item(self.issue_categories)
self.add_item(self.issue_extra_info)
self.add_item(self.developer_role)
async def on_submit(self, interaction: discord.Interaction):
name_type = self.product.value.split("\n")
# TODO: strip() and remove empty lines
self.config.github_repo = self.repo.value
self.config.product_name = name_type[0]
self.config.product_type = name_type[1] if len(name_type) > 1 else ""
self.config.issue_categories = self.issue_categories.value.split("\n")
self.config.issue_extra_info = self.issue_extra_info.value.split("\n")
self.config.discord_developer_role = self.developer_role.value
self.submitted = True
if await check_config(interaction, self.config):
state.set_config(interaction.guild.id, self.config)
await interaction.response.send_message(f"Setup complete", ephemeral=True)
class BugCorrect(discord.ui.Modal, title="Correct bug report"):
comment = discord.ui.TextInput(label="Comment",
style=discord.TextStyle.long,
placeholder="",
max_length=400,
required=False)
submitted = False
async def on_submit(self, interaction: discord.Interaction):
self.submitted = True
await interaction.response.send_message(f"Preparing bug report...", ephemeral=True)
class Confirm(discord.ui.View):
def __init__(self):
super().__init__()
self.value = None
self.comment = None
@discord.ui.button(label='Confirm', style=discord.ButtonStyle.green)
async def confirm(self, interaction: discord.Interaction, button: discord.ui.Button):
self.value = "confirm"
self.stop()
@discord.ui.button(label='Regenerate', style=discord.ButtonStyle.blurple)
async def regenerate(self, interaction: discord.Interaction, button: discord.ui.Button):
self.value = "regenerate"
self.stop()
@discord.ui.button(label='Correct', style=discord.ButtonStyle.blurple)
async def correct(self, interaction: discord.Interaction, button: discord.ui.Button):
self.value = "correct"
correct_modal = BugCorrect()
await interaction.response.send_modal(correct_modal)
while not correct_modal.submitted:
await asyncio.sleep(1)
self.comment = correct_modal.comment.value
self.stop()
@discord.ui.button(label='Cancel', style=discord.ButtonStyle.grey)
async def cancel(self, interaction: discord.Interaction, button: discord.ui.Button):
self.value = "cancel"
self.stop()
@client.event
async def on_ready():
# await client.tree.sync()
state.init()
_logger.info(f"{client.user} is ready and online!")
def extract_message_link_ids(url) -> tuple[int, int, int]:
match = re.match(r"https://discord.com/channels/(\d+)/(\d+)/(\d+)", url)
if match:
return (int(match.group(1)), int(match.group(2)), int(match.group(3)))
return None
async def get_message_from_link(message_link, wanted_guild_id) -> discord.Message:
guild_id, channel_id, message_id = extract_message_link_ids(message_link)
if not guild_id or guild_id != wanted_guild_id:
return None
channel = client.get_channel(channel_id)
return await channel.fetch_message(message_id)
@client.tree.command(name="setup")
async def setup(interaction: discord.Interaction):
setup_modal = Setup()
await setup_modal.populate(interaction.guild.id)
await interaction.response.send_modal(setup_modal)
@client.tree.command(name="bug")
@app_commands.describe(message_link='Message link to start reading from',
bug_hint='Hint with regards to the bug')
async def new_report(interaction: discord.Integration, message_link: str, bug_hint: Optional[str] = "None"):
await interaction.response.defer(ephemeral=True, thinking=True)
if not await check_config(interaction, await state.get_config(interaction.guild.id)):
return
followup = interaction.followup
guild_id = interaction.guild.id
followup_message = None
message = await get_message_from_link(message_link, interaction.guild.id)
if not message:
await followup.send(f"Invalid message link!", ephemeral=True)
return
followup_message = await followup.send(f"Preparing bug report...", ephemeral=True)
while await state.get_busy(guild_id):
await asyncio.sleep(10)
await state.set_busy(guild_id, True)
try:
combined_history = await chatproc.get_history(guild_id, message, 50, 3)
analysis_suite = await state.get_analysis_suite(guild_id)
issue_analysis = await analysis_suite.analyse_issue(combined_history, bug_hint)
if issue_analysis == {}:
await followup_message.edit(content=f"No issues found in the chat log!")
await state.set_busy(guild_id, False)
return
issue_title, issue_md = analysis_suite.make_markdown(issue_analysis)
while True:
confirm_view = Confirm()
await followup_message.edit(content=f"Confirm bug report?\n\n{issue_md}", view=confirm_view)
await confirm_view.wait()
await followup_message.edit(view=None)
match confirm_view.value:
case "confirm":
issue_url = await file_issue(guild_id, issue_title, issue_md)
await followup.send(f"Bug report filed:\n{issue_url}")
break
case "regenerate":
await followup_message.edit(content="Regenerating bug report...", view=None)
issue_analysis = await analysis_suite.analyse_issue(combined_history, bug_hint)
issue_title, issue_md = analysis_suite.make_markdown(issue_analysis)
case "correct":
await followup_message.edit(content="Correcting bug report...", view=None)
issue_analysis = await analysis_suite.correct_analysis(issue_analysis, confirm_view.comment)
issue_title, issue_md = analysis_suite.make_markdown(issue_analysis)
case "cancel":
await followup_message.edit(content="Bug report cancelled!", view=None)
break
except Exception as e:
await followup.send("There was an error filing the bug report, please contact bot admin.", ephemeral=True)
_logger.exception(f"Error filing bug report:\n{e}")
await state.set_busy(guild_id, False)
if __name__ == "__main__":
client.run(os.getenv('DISCORD_TOKEN'))