Rewrite of newServer.py to addServer.py. Squashed bugs. Made it so only server owners can add their server.

This commit is contained in:
officereso 2019-12-20 23:03:26 -06:00
parent 10391e4eac
commit fce26e0664
6 changed files with 120 additions and 7 deletions

View File

@ -0,0 +1,99 @@
from discord.ext import commands
import discord
import pymysql
import asyncio
con = pymysql.connect(host='192.168.1.52',
user='Quentin',
password='kaPl0wskii',
db='serverIDs',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
class AddServer(commands.Cog):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_message(self, ctx):
if isinstance(ctx.channel, discord.DMChannel):
if ctx.content.lower() == "add server":
await ctx.channel.send("Send me an invite to the server. Say cancel anytime to cancel.")
try:
invite = await self.client.wait_for("message", check=lambda message: message.author == ctx.author
and message.channel == ctx.channel, timeout=20)
if invite.content == "cancel":
await ctx.channel.send("Process canceled")
else:
is_invite = invite.content.find("discord.gg")
if is_invite > 0:
invite = await self.client.fetch_invite(invite.content, with_counts=True)
with con:
cur = con.cursor()
server_exists = cur.execute(f"SELECT ID FROM servers WHERE ID = {invite.guild.id}")
if server_exists == 0:
try:
if invite.guild.owner == ctx.author:
if invite.max_uses is None:
await ctx.channel.send("Please send a description of the server")
description = await self.client.wait_for("message", check=lambda message:
message.author == ctx.author and
message.channel == ctx.channel,
timeout=60)
if description.content == "cancel":
await ctx.channel.send("Process canceled. If need be have "
"this pretyped")
else:
await ctx.channel.send("Please send a list of tags separated by a "
"comma(,) no spaces")
tags = await self.client.wait_for("message", check=lambda message:
message.author == ctx.author
and message.channel == ctx.channel)
if tags == "cancel":
await ctx.channel.send("Process canceled")
else:
try:
with con:
cur = con.cursor()
cur.execute(f"INSERT INTO servers "
f"(ID, name, tags, invite, description) "
f"VALUES ({invite.guild.id},'"
f"{invite.guild.name}','"
f"{tags.content}','{invite.code}','"
f"{description.content}')")
await ctx.channel.send("Server added successfully")
except Exception as e:
await ctx.channel.send("An error has occurred and will be fixed"
"shortly.")
print(e)
await self.client.get_channel(656997125627969546).send(e)
await self.client.\
get_channel(656997125627969546).\
send("<@305589587215122432>")
except AttributeError:
await ctx.channel.send("Cannot verify server ownership. "
"Either you are not the guild owner or"
"the bot has not been added to verify ownership.\n"
"Add the bot here : https://discordapp.com/api/oauth2"
"/authorize?client_id=501485906801459200&permissions=1024"
"&scope=bot\n"
"Once the bot is added restart the verification process "
"using ``add server`` in this DM\n"
"After ownership is verified the bot can be removed.")
else:
await ctx.channel.send("It seems this server is already in our databases. "
"It can be removed with ``remove server``")
else:
await ctx.channel.send("Invalid invite.")
except asyncio.TimeoutError:
await ctx.channel.send("Process timeout.")
def setup(client):
client.add_cog(AddServer(client))

View File

@ -21,22 +21,24 @@ class NewServer(commands.Cog):
if ctx.content.lower() == "add server":
await ctx.channel.send("Send an invite to the server")
invite = await self.client.wait_for("message", check=lambda message: message.author == ctx.author and message.channel == ctx.channel)
isInvite = invite.content.find("discord.gg")
if isInvite > 0:
is_invite = invite.content.find("discord.gg")
if is_invite > 0:
invite = await self.client.fetch_invite(invite.content, with_counts=True)
print(invite.max_uses)
if not (invite.max_uses is None):
await ctx.channel.send("Please send a invite with no max uses")
elif invite.inviter != ctx.author:
await ctx.channel.send("Please be the creator of the invite")
elif invite.inviter != invite.guild.owner:
await ctx.channel.send("Please be the owner of this server")
else:
await ctx.channel.send("Please send a description of the server")
xdescription = await self.client.wait_for("message", check=lambda message: message.author == ctx.author and message.channel == ctx.channel)
description = await self.client.wait_for("message", check=lambda message: message.author == ctx.author and message.channel == ctx.channel)
await ctx.channel.send("Please send a list of tags separated by a comma(,) no spaces")
xtags = await self.client.wait_for("message", check=lambda message: message.author == ctx.author and message.channel == ctx.channel)
tags = await self.client.wait_for("message", check=lambda message: message.author == ctx.author and message.channel == ctx.channel)
with con:
cur = con.cursor()
cur.execute(f"INSERT INTO servers (ID, name, tags, invite, description) VALUES ({invite.guild.id},'{invite.guild.name}','{xtags.content}','{invite.code}','{xdescription.content}')")
cur.execute(f"INSERT INTO servers (ID, name, tags, invite, description) VALUES ({invite.guild.id},'{invite.guild.name}','{tags.content}','{invite.code}','{description.content}')")
else:
await ctx.channel.send("Invalid invite")

View File

@ -0,0 +1,12 @@
import pymysql
con = pymysql.connect(host='192.168.1.52',
user='Quentin',
password='kaPl0wskii',
db='serverIDs',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
with con:
cur = con.cursor()
print(cur.execute("SELECT ID FROM servers WHERE ID = 1"))

View File

@ -1 +1 @@
ALTER TABLE servers MODIFY ID BIGINT;
SELECT ID FROM servers WHERE ID = 1;

View File

View File

@ -2,7 +2,7 @@ from discord.ext.commands import Bot
cogs = ["Modules.TGH.automod.automod", "Modules.TGH.automod.ouat",
"Modules.SSC.autoReply.Join", "Modules.SSC.autoReply.Leave",
"Modules.Hub.Database.newServer"]
"Modules.Hub.Database.addServer"]
prefix = 'o!'