Skip to content
This repository was archived by the owner on Mar 24, 2023. It is now read-only.

Commit eba4475

Browse files
committed
Updated bot to support new API
1 parent 3b8af18 commit eba4475

File tree

2 files changed

+60
-146
lines changed

2 files changed

+60
-146
lines changed

main.py

Lines changed: 56 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,158 +1,74 @@
11
import discord
2-
from discord.ext import commands
3-
from discord_slash import SlashCommand
2+
from discord import app_commands
43
from dotenv import load_dotenv
54
import os
65
import requests
7-
import json
8-
import sentry_sdk
96
import re
10-
11-
12-
# Basic setup
13-
description = "Official StopModReposts bot"
14-
bot = commands.Bot(command_prefix='/', description=description, intents=discord.Intents.all())
15-
slash = SlashCommand(bot, sync_commands=True)
16-
# 785935453719101450 TESTING
17-
guild_ids = [463457129588850699]
7+
import json
188

199
load_dotenv()
2010
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
21-
GITHUB_USER = "smr-bot"
22-
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
23-
24-
sentry_sdk.init(
25-
"https://f6ae76c36eec4b6ab34ac46cba61d358@o309026.ingest.sentry.io/5583030",
26-
traces_sample_rate=1.0
27-
)
28-
11+
SMR_GUILD_ID = "463457129588850699"
12+
client = discord.Client(application_id="785933469531504690", intents=discord.Intents.all())
13+
tree = app_commands.CommandTree(client)
2914

30-
# Bot functions
3115
def checklist(url):
3216
try:
33-
r = requests.get('https://api.stopmodreposts.org/sites.txt')
34-
except:
35-
errormsg = "REQUEST FAILED"
36-
return errormsg
37-
38-
if r.status_code == 200:
39-
if url in r.text:
40-
return True
41-
else:
42-
return False
43-
else:
44-
errormsg = "REQUEST FAILED WITH STATUS CODE " + str(r.status_code)
45-
return errormsg
46-
47-
def createissue(title, body=None, labels=None):
48-
'''Create an issue on github.com using the given parameters.'''
49-
url = 'https://api.github.com/repos/StopModReposts/Illegal-Mod-Sites/issues'
50-
session = requests.Session()
51-
session.auth = (GITHUB_USER, GITHUB_TOKEN)
52-
# Create our issue
53-
issue = {'title': title,
54-
'body': body,
55-
'labels': labels}
56-
headers = {'Accept': 'application/vnd.github.v3+json'}
57-
# Add the issue to our repository
58-
r = session.post(url, json.dumps(issue), headers)
59-
if r.status_code == 201:
60-
jsondata = json.loads(r.text)
61-
return jsondata.get("number")
62-
else:
63-
return 0
64-
65-
def checkissue(num):
66-
url = 'https://api.github.com/repos/StopModReposts/Illegal-Mod-Sites/issues/{0}'.format(num)
67-
r = requests.get(url)
68-
69-
if r.status_code == 200:
70-
jsondata = json.loads(r.text)
71-
return r.status_code, jsondata.get("state"), jsondata.get("title")
17+
r = requests.get("https://api.stopmodreposts.org/sites.txt")
18+
if r.status_code != 200:
19+
return f"⚠️ Failed with status code {r.status_code}"
20+
except ConnectionError:
21+
return "⚠️ Connection failed"
22+
23+
if url in r.text:
24+
return True
7225
else:
73-
return r.status_code, "N/A", "N/A"
74-
26+
return False
27+
28+
@tree.command(name="sample", description="Example command")
29+
async def sample(interaction: discord.Interaction):
30+
await interaction.response.send_message("Hello!")
31+
32+
@tree.command(name="check", description="Check if a site is listed")
33+
async def check(interaction: discord.Interaction, url: str):
34+
url = re.search("([a-z0-9A-Z]\.)*[a-z0-9-]+\.([a-z0-9]{2,24})+(\.co\.([a-z0-9]{2,24})|\.([a-z0-9]{2,24}))*",
35+
url).group(0)
36+
37+
# List status
38+
list_status = checklist(url)
39+
if type(list_status) != bool:
40+
await interaction.response.send_message(f"List Error: {list_status}")
41+
elif list_status is False:
42+
await interaction.response.send_message(f"❌ **{url}** is not on our list")
43+
elif list_status is True:
44+
await interaction.response.send_message(f"✅ **{url}** is on our list")
45+
46+
@tree.command(name="submit", description="Submit a site for review")
47+
async def submit(interaction: discord.Interaction, url: str, description: str):
48+
url = re.search("([a-z0-9A-Z]\.)*[a-z0-9-]+\.([a-z0-9]{2,24})+(\.co\.([a-z0-9]{2,24})|\.([a-z0-9]{2,24}))*",
49+
url).group(0)
50+
data = {
51+
"domain": str(url),
52+
"description": f"VIA DC - {description}"
53+
}
7554

76-
# Bot commands
77-
@bot.event
78-
async def on_ready():
79-
guild_count = 0
80-
81-
print("Logged in as")
82-
print(bot.user.name)
83-
print(bot.user.id)
84-
print("------")
85-
86-
for guild in bot.guilds:
87-
print("{0} : {1}".format(guild.id, guild.name))
88-
guild_count = guild_count + 1
89-
90-
await bot.change_presence(activity=discord.Game(name="on " + str(guild_count) + " servers | /help"))
91-
92-
print("Bot is in " + str(guild_count) + " guilds")
93-
94-
95-
@slash.slash(name="ping", description="Test command which returns the bot's ping", guild_ids=guild_ids)
96-
async def ping(ctx):
97-
await ctx.send("Pong! Bot latency: {0}".format(bot.latency))
98-
99-
100-
@slash.slash(name="submit", description="Submit a URL for review", guild_ids=guild_ids)
101-
async def submit(ctx, url: str, description: str):
102-
url = re.search("([a-z0-9A-Z]\.)*[a-z0-9-]+\.([a-z0-9]{2,24})+(\.co\.([a-z0-9]{2,24})|\.([a-z0-9]{2,24}))*", url).group(0)
103-
if description == None:
104-
await ctx.send("Please add a description after the URL.")
105-
else:
106-
check = checklist(url)
107-
if check is False:
108-
num = createissue("New site to add: "+url, description+" *Automated Issue - submitted by "+str(ctx.author)+"*", ["addition"])
109-
link = "https://github.com/StopModReposts/Illegal-Mod-Sites/issues/" + str(num)
110-
embed=discord.Embed(title="Click here to view your issue", url=link, description="I've created a GitHub issue where you can track the progress of your request. You can also use `/status [issuenumber]` to get the status of your request.", color=0x00ff80)
111-
embed.set_author(name="Thanks for your report!")
112-
embed.add_field(name="Site", value=url, inline=True)
113-
embed.add_field(name="Issue Number", value=num, inline=True)
114-
embed.add_field(name="Description", value=description, inline=False)
115-
embed.set_footer(text="Submitted by {0}".format(ctx.author))
116-
await ctx.send(embed=embed)
117-
elif check is True:
118-
await ctx.send("**{0}** is already on our lists.".format(url))
119-
else:
120-
await ctx.send("Error with your request - {0}".format(check))
121-
122-
123-
@slash.slash(name="check", description="Check if a website is already on our lists", guild_ids=guild_ids)
124-
async def check(ctx, url: str):
125-
url = re.search("([a-z0-9A-Z]\.)*[a-z0-9-]+\.([a-z0-9]{2,24})+(\.co\.([a-z0-9]{2,24})|\.([a-z0-9]{2,24}))*", url).group(0)
126-
check = checklist(url)
127-
if check is False:
128-
await ctx.send(":x: **{0}** is not on our list.".format(url))
129-
elif check is True:
130-
await ctx.send(":white_check_mark: **{0}** is on our list.".format(url))
131-
else:
132-
await ctx.send("Error with your request - {0}".format(check))
55+
try:
56+
r = requests.post("https://report.stopmodreposts.org/api/v1/report?falsepositive=false", json=data)
57+
except ConnectionError:
58+
return "⚠️ Connection failed"
13359

60+
res = json.loads(r.text)
13461

135-
@slash.slash(name="status", description="Check the status of a GitHub issue", guild_ids=guild_ids)
136-
async def status(ctx, num: int):
137-
code, status, title = checkissue(num)
138-
if code == 200:
139-
if status == "open":
140-
embed_status = "❌ Unreviewed (open)"
141-
embed_color = 0xff0000
142-
else:
143-
embed_status = "✅ Reviewed (closed)"
144-
embed_color = 0x00ff80
145-
link = "https://github.com/StopModReposts/Illegal-Mod-Sites/issues/" + str(num)
146-
embed=discord.Embed(title="Click here to view the issue", url=link, description="Here's the issue status you've requested.", color=embed_color)
147-
embed.set_author(name="Issue status")
148-
embed.add_field(name="Issue Number", value=num, inline=True)
149-
embed.add_field(name="Status", value=embed_status, inline=True)
150-
await ctx.send(embed=embed)
151-
elif code == 404:
152-
await ctx.send("This issue doesn't exist.")
62+
if r.status_code == 201:
63+
await interaction.response.send_message(f"✅ **{url}** reported! Thanks!")
64+
elif r.status_code == 409 or r.status_code == 400:
65+
await interaction.response.send_message(f"⚠️ Failed to report **{url}**!\nDetail: `{res['detail']}`")
15366
else:
154-
await ctx.send("Error with your request - Status code: {0}".format(code))
67+
await interaction.response.send_message(f"⚠️ Failed with status code {r.status_code}")
15568

69+
async def main():
70+
async with client:
71+
await tree.sync()
72+
await client.start(DISCORD_TOKEN)
15673

157-
# Run bot
158-
bot.run(DISCORD_TOKEN)
74+
client.run(DISCORD_TOKEN)

requirements.txt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
requests~=2.25.1
2-
python-dotenv~=0.15.0
3-
discord.py~=1.5.1
4-
discord-py-slash-command~=1.1.2
5-
sentry-sdk~=1.1.0
6-
aiohttp>=3.7.4 # not directly required, pinned by Snyk to avoid a vulnerability
1+
requests
2+
python-dotenv
3+
discord.py
4+
aiohttp

0 commit comments

Comments
 (0)