|
| 1 | +import discord |
| 2 | +from discord.ext import commands |
| 3 | +from dotenv import load_dotenv |
| 4 | +import os |
| 5 | +import requests |
| 6 | +import json |
| 7 | + |
| 8 | + |
| 9 | +# Basic setup |
| 10 | +description = "Official StopModReposts bot" |
| 11 | +bot = commands.Bot(command_prefix='/', description=description) |
| 12 | + |
| 13 | +load_dotenv() |
| 14 | +DISCORD_TOKEN = os.getenv("DISCORD_TOKEN") |
| 15 | + |
| 16 | +GITHUB_USER = "smr-bot" |
| 17 | +GITHUB_TOKEN = os.getenv("GITHUB_TOKEN") |
| 18 | + |
| 19 | + |
| 20 | +# Bot functions |
| 21 | +def checklist(url): |
| 22 | + try: |
| 23 | + r = requests.get('https://api.stopmodreposts.org/sites.txt') |
| 24 | + except: |
| 25 | + errormsg = "REQUEST FAILED" |
| 26 | + return errormsg |
| 27 | + |
| 28 | + if r.status_code == 200: |
| 29 | + if url in r.text: |
| 30 | + return True |
| 31 | + else: |
| 32 | + return False |
| 33 | + else: |
| 34 | + errormsg = "REQUEST FAILED WITH STATUS CODE " + str(r.status_code) |
| 35 | + return errormsg |
| 36 | + |
| 37 | + |
| 38 | +def createissue(title, body=None, labels=None): |
| 39 | + '''Create an issue on github.com using the given parameters.''' |
| 40 | + # Our url to create issues via POST |
| 41 | + url = 'https://api.github.com/repos/StopModReposts/Illegal-Mod-Sites/issues' |
| 42 | + # Create an authenticated session to create the issue |
| 43 | + session = requests.Session() |
| 44 | + session.auth = (GITHUB_USER, GITHUB_TOKEN) |
| 45 | + # Create our issue |
| 46 | + issue = {'title': title, |
| 47 | + 'body': body, |
| 48 | + 'labels': labels} |
| 49 | + headers = {'Accept': 'application/vnd.github.v3+json'} |
| 50 | + # Add the issue to our repository |
| 51 | + r = session.post(url, json.dumps(issue), headers) |
| 52 | + if r.status_code == 201: |
| 53 | + print ('Successfully created Issue {0}'.format(title)) |
| 54 | + jsondata = json.loads(r.text) |
| 55 | + print(jsondata.get("number")) |
| 56 | + return jsondata.get("number") |
| 57 | + else: |
| 58 | + print ('Could not create Issue {0}'.format(title)) |
| 59 | + print ('Response:', r.content) |
| 60 | + return 0 |
| 61 | + |
| 62 | + |
| 63 | +# Bot commands |
| 64 | +@bot.event |
| 65 | +async def on_ready(): |
| 66 | + guild_count = 0 |
| 67 | + |
| 68 | + print("Logged in as") |
| 69 | + print(bot.user.name) |
| 70 | + print(bot.user.id) |
| 71 | + print("------") |
| 72 | + |
| 73 | + for guild in bot.guilds: |
| 74 | + print("{0} : {1}".format(guild.id, guild.name)) |
| 75 | + guild_count = guild_count + 1 |
| 76 | + |
| 77 | + await bot.change_presence(activity=discord.Game(name="on " + str(guild_count) + " servers | /help")) |
| 78 | + |
| 79 | + print("Bot is in " + str(guild_count) + " guilds") |
| 80 | + |
| 81 | + |
| 82 | +@bot.command(description="Test command which returns ping time", help="Test command which returns ping time") |
| 83 | +async def ping(ctx): |
| 84 | + await ctx.send("Pong! Bot latency: {0}".format(bot.latency)) |
| 85 | + |
| 86 | + |
| 87 | +@bot.command(description="Submit a URL for review", help="Submit a URL for review") |
| 88 | +async def submit(ctx, url: str, *, args=None): |
| 89 | + if args == None: |
| 90 | + await ctx.send("Please add a description after the URL.") |
| 91 | + elif "http" in url: |
| 92 | + await ctx.send("Please only submit the Domain without http:// or https://.") |
| 93 | + else: |
| 94 | + check = checklist(url) |
| 95 | + if check == False: |
| 96 | + num = createissue("New site to add: "+url, args+" *Automated Issue*", ["addition"]) |
| 97 | + link = "https://github.com/StopModReposts/Illegal-Mod-Sites/issues/" + str(num) |
| 98 | + await ctx.send("Your report for **{0}** has been received. I've created a GitHub issue (#{1} - <{2}>) where you can track the progress of your request. ".format(url, num, link)) |
| 99 | + elif check == True: |
| 100 | + await ctx.send("**{0}** is already on our lists.".format(url)) |
| 101 | + else: |
| 102 | + await ctx.send("Error with your request - {0}".format(check)) |
| 103 | + |
| 104 | + |
| 105 | +# Run bot |
| 106 | +bot.run(DISCORD_TOKEN) |
0 commit comments