Skip to content

Commit

Permalink
Switch to motor from pymongo and command edit stuff works
Browse files Browse the repository at this point in the history
  • Loading branch information
carolinaisslaying committed Apr 18, 2020
1 parent d10d818 commit 0005e64
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 9 deletions.
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,11 @@ dmypy.json
.pyre/

# Configuration
settings.json
settings.json

# Redis
dump.rdb

# IDEs
.vscode/
.idea/
23 changes: 20 additions & 3 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
import colouredlogs, logging
import datetime
import discord
import pymongo
from motor.motor_asyncio import AsyncIOMotorClient
import json

from colorama import Fore, init
from discord.ext import commands, tasks
from ext.context import EditingContext

init()
colouredlogs.install()
Expand All @@ -34,8 +35,7 @@

fr = Fore.RESET
logging.info("Starting bot")
MongoClient = pymongo.MongoClient(settings["mongo"]["uri"])
db = MongoClient[settings["mongo"]["db"]]
db = AsyncIOMotorClient(settings["mongo"]["uri"])[settings["mongo"]["db"]]
botExtensions = [
"cogs.help",
"cogs.utility",
Expand All @@ -59,6 +59,7 @@ async def get_prefix(bot, message):
bot.remove_command("help")
bot.db = db
bot.settings = settings
bot.cmd_edits = {}

if __name__ == "__main__":
for ext in botExtensions:
Expand Down Expand Up @@ -148,6 +149,22 @@ async def on_member_join(member):
if botCount >= 1:
await member.add_roles(discord.Object(id=int(settings["roles"]["developer"])), reason="User is a Verified Developer on the website.")

@bot.event
async def on_command_error(ctx, error):
if error in (discord.Forbidden, commands.BotMissingPermissions, commands.CommandNotFound, commands.CheckFailure):
pass

@bot.event
async def on_message(msg):
if not msg.author.bot:
ctx = await bot.get_context(msg, cls=EditingContext)
await bot.invoke(ctx)


@bot.event
async def on_message_edit(old_msg, new_msg):
if not old_msg.author.bot and new_msg.content is not old_msg.content:
ctx = await bot.get_context(new_msg, cls=EditingContext)
await bot.invoke(ctx)

bot.run(settings["token"], bot=True, reconnect=True)
27 changes: 23 additions & 4 deletions cogs/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def embedColour():
if user == None:
user = ctx.author

dbUser = self.bot.db["users"].find_one({"id": str(user.id)})
dbUser = await self.bot.db.users.find_one({"id": str(user.id)})

if dbUser:
embed = discord.Embed(colour=await embedColour())
Expand Down Expand Up @@ -81,10 +81,10 @@ async def embedColour():
if bot == None:
return await ctx.send(f"{self.bot.settings['formats']['error']} **Invalid argument:** You need to mention or provide an ID of a bot.")

dbBot = self.bot.db["bots"].find_one({"id": str(bot.id)})
dbBot = await self.bot.db.bots.find_one({"id": str(bot.id)})

if dbBot:
botOwner = self.bot.db["users"].find_one({"id": dbBot["owner"]["id"]})
botOwner = await self.bot.db.users.find_one({"id": dbBot["owner"]["id"]})

embed = discord.Embed(colour=await embedColour())

Expand Down Expand Up @@ -120,7 +120,7 @@ async def embedColour():
if bot == None:
return await ctx.send(f"{self.bot.settings['formats']['error']} **Invalid argument:** You need to mention or provide an ID of a bot.")

dbBot = self.bot.db["bots"].find_one({"id": str(bot.id)})
dbBot = await self.bot.db.bots.find_one({"id": str(bot.id)})

if dbBot:
if dbBot["owner"]["id"] == str(ctx.author.id):
Expand All @@ -138,6 +138,25 @@ async def embedColour():
else:
return await ctx.send(f"{self.bot.settings['formats']['error']} **Invalid bot:** I could not find the bot you specified in my database.")

@commands.command(name="cssreset", aliases=["resetcss"], usage="cssreset", help="Allows you to reset your custom css if you've broken something.", hidden=False)
async def css_reset(self, ctx):
async with ctx.channel.typing():
dbUser = await self.bot.db.users.find_one({"id": str(ctx.author.id)})

if dbUser:
await self.bot.db.users.update_one({"id": str(ctx.author.id)}, {
"$set": {
"profile.css": ""
}
})

return await ctx.send(f"{self.bot.settings['formats']['success']} **Success:** Your custom css was reset.")
else:
return await ctx.send(f"{self.bot.settings['formats']['error']} **Unknown account:** You need to have authenticated on our website before to use this command.")




def setup(bot):
bot.add_cog(UtilityCog(bot))

19 changes: 19 additions & 0 deletions ext/context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from discord.ext import commands

class EditingContext(commands.Context):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

async def send(self, content=None, *, tts=False, embed=None, file=None, files=None, delete_after=None, nonce=None, allowed_mentions=None):
if file or files:
return await super().send(content=content, tts=tts, embed=embed, file=file, files=files, delete_after=delete_after, nonce=nonce, allowed_mentions=allowed_mentions)
reply = None
try:
reply = self.bot.cmd_edits[self.message.id]
except KeyError:
pass
if reply:
return await reply.edit(content=content, embed=embed, delete_after=delete_after)
msg = await super().send(content=content, tts=tts, embed=embed, file=file, files=files, delete_after=delete_after, nonce=nonce, allowed_mentions=allowed_mentions)
self.bot.cmd_edits[self.message.id] = msg
return msg
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ chardet==3.0.4
colorama==0.4.3
colouredlogs==10.0.1
constantly==15.1.0
discord.py==1.3.2
git+https://github.com/Rapptz/discord.py
dnspython==1.16.0
docopt==0.6.2
furl==2.1.0
Expand All @@ -23,6 +23,7 @@ incremental==17.5.0
Jinja2==2.11.1
jishaku==1.18.2.188
MarkupSafe==1.1.1
motor==2.1.0
multidict==4.7.5
orderedmultidict==1.0.1
pkg-resources==0.0.0
Expand Down

0 comments on commit 0005e64

Please sign in to comment.