-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
109 lines (84 loc) · 2.75 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import os
import json
import discord
from discord.ext import commands
from attackerkb_api import AttackerKB
import utils.printer as printer
# Strings and config.
queryMaxTopics = 5
error = "Sorry, I could not find anything for this {}!"
token = os.environ.get('DISCORD_TOKEN')
api_key = os.environ.get('AKB_API_KEY')
if token == None or api_key == None:
# if enviroment variables have not been set
# stop execution
print("DISCORD_TOKEN or AKB_API_KEY enviroment variables have not been set")
exit(1)
# Setting up the bot and prefix.
prefix = "!akb "
bot = commands.Bot(command_prefix=prefix)
# Loads the bot's activity status.
status = prefix+"help"
# AKB api.
api = AttackerKB(api_key)
# Logging the starting of the bot into the console.
@bot.event
async def on_ready():
# Sets activity message.
await bot.change_presence(activity=discord.Game(status))
# Removes default help command.
print("\nLogged in as {0.user}".format(bot)+"\n")
# Commands.
## Ping command.
@bot.command(description="Ping the bot.")
async def ping(ctx):
await ctx.send("Pong!")
## Topic command.
@bot.command(description="Get a topic.")
async def topic(ctx, id):
try:
query = api.get_single_topic(id)
message = printer.topic(query)
except:
message = printer.not_found(error.format("topic"))
await ctx.send(embed=message)
## Assessment command.
@bot.command(description="Get an assessment.")
async def assessment(ctx, id):
try:
query = api.get_single_assessment(id)
author_name = api.get_single_contributor(query["editorId"])["username"]
topic_name = api.get_single_topic(query["topicId"])["name"][:15]
message = printer.assessment(query, author_name, topic_name)
except:
message = printer.not_found(error.format("assessment"))
await ctx.send(embed=message)
## CVE command.
@bot.command(description="Get a CVE.")
async def cve(ctx, id):
try:
query = api.get_topics(name=id)
message = printer.topic(query[0])
except:
message = printer.not_found(error.format("CVE"))
await ctx.send(embed=message)
## User command.
@bot.command(description="Get a user.")
async def user(ctx, id):
try:
query = api.get_single_contributor(id)
message = printer.user(query)
except:
message = printer.not_found(error.format("user"))
await ctx.send(embed=message)
## Query command.
@bot.command(description="Query for topics.")
async def query(ctx, *, keywords):
try:
query = api.get_topics(q=keywords, size=queryMaxTopics)
message = printer.topic_list(keywords, query, queryMaxTopics)
except:
message = printer.not_found(error.format("query"))
await ctx.send(embed=message)
# Starting the bot.
bot.run(token)