Merge pull request #5 from bobloy/stealemoji_develop

Forgot info file and errors
pull/6/head
bobloy 7 years ago committed by GitHub
commit c36f90698e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,20 @@
{
"author": [
"Bobloy"
],
"bot_version": [
3,
0,
0
],
"description": "Steals custom emojis the bot sees and moves them to an emoji server",
"hidden": true,
"install_msg": "Thank you for installing StealEmoji",
"requirements": [],
"short": "Steals custom emojis",
"tags": [
"bobloy",
"utils",
"emoji"
]
}

@ -1,18 +1,11 @@
import asyncio
import aiohttp import aiohttp
from urllib.parse import urlparse
import os
from typing import List, Union
import discord import discord
from discord.ext import commands from discord.ext import commands
from redbot.core import Config from redbot.core import Config, RedContext
from redbot.core.bot import Red from redbot.core.bot import Red
# from redbot.core import data_manager
async def fetch_img(session, url): async def fetch_img(session, url):
with aiohttp.Timeout(10): with aiohttp.Timeout(10):
@ -20,35 +13,35 @@ async def fetch_img(session, url):
assert response.status == 200 assert response.status == 200
return await response.read() return await response.read()
class StealEmoji: class StealEmoji:
""" """
This cog steals emojis and creates servers for them This cog steals emojis and creates servers for them
""" """
default_stolemoji = {
"guildbank": None,
"name": None,
"require_colons": False,
"managed": False,
"guild_id": None,
"url": None,
"animated": False
}
def __init__(self, red: Red): def __init__(self, red: Red):
self.bot = red self.bot = red
self.config = Config.get_conf(self, identifier=11511610197108101109111106105) self.config = Config.get_conf(self, identifier=11511610197108101109111106105)
default_global = { default_global = {
"stolemoji": {}, "stolemoji": {},
"guildbanks": [], "guildbanks": [],
"on": False "on": False
} }
default_stolemoji = {
"guildbank": None,
"name": None,
"require_colons": False,
"managed": False,
"guild_id": None,
"url": None,
"animated": False
}
self.config.register_global(**default_global) self.config.register_global(**default_global)
@commands.group() @commands.group()
async def stealemoji(self, ctx: commands.Context): async def stealemoji(self, ctx: RedContext):
""" """
Base command for this cog. Check help for the commands list. Base command for this cog. Check help for the commands list.
""" """
@ -58,109 +51,106 @@ class StealEmoji:
@stealemoji.command(name="collect") @stealemoji.command(name="collect")
async def se_collect(self, ctx): async def se_collect(self, ctx):
"""Toggles whether emoji's are collected or not""" """Toggles whether emoji's are collected or not"""
currSetting = await self.config.on() curr_setting = await self.config.on()
await self.config.on.set(not currSetting) await self.config.on.set(not curr_setting)
await ctx.send("Collection is now "+str(not currSetting)) await ctx.send("Collection is now " + str(not curr_setting))
@stealemoji.command(name="bank") @stealemoji.command(name="bank")
async def se_bank(self, ctx): async def se_bank(self, ctx):
"""Add current server as emoji bank""" """Add current server as emoji bank"""
await ctx.send("This will upload custom emojis to this server\n" await ctx.send("This will upload custom emojis to this server\n"
"Are you sure you want to make the current server an emoji bank? (y/n)") "Are you sure you want to make the current server an emoji bank? (y/n)")
def check(m): def check(m):
return m.content.upper() in ["Y","YES","N","NO"] and m.channel == ctx.channel and m.author == ctx.author return m.content.upper() in ["Y", "YES", "N", "NO"] and m.channel == ctx.channel and m.author == ctx.author
msg = await self.bot.wait_for('message', check=check) msg = await self.bot.wait_for('message', check=check)
if msg.content in ["N","NO"]: if msg.content in ["N", "NO"]:
await ctx.send("Cancelled") await ctx.send("Cancelled")
return return
async with self.config.guildbanks() as guildbanks: async with self.config.guildbanks() as guildbanks:
guildbanks.append(ctx.guild.id) guildbanks.append(ctx.guild.id)
await ctx.send("This server has been added as an emoji bank") await ctx.send("This server has been added as an emoji bank")
async def on_reaction_add(self, reaction: discord.Reaction, user: discord.User): async def on_reaction_add(self, reaction: discord.Reaction, user: discord.User):
"""Event handler for reaction watching""" """Event handler for reaction watching"""
if not reaction.custom_emoji: if not reaction.custom_emoji:
print("Not a custom emoji") print("Not a custom emoji")
return return
if not (await self.config.on()): if not (await self.config.on()):
print("Collecting is off") print("Collecting is off")
return return
emoji = reaction.emoji emoji = reaction.emoji
if emoji in self.bot.emojis: if emoji in self.bot.emojis:
print("Emoji already in bot.emojis") print("Emoji already in bot.emojis")
return return
# This is now a custom emoji that the bot doesn't have access to, time to steal it # This is now a custom emoji that the bot doesn't have access to, time to steal it
# First, do I have an available guildbank? # First, do I have an available guildbank?
guildbank = None guildbank = None
banklist = await self.config.guildbanks() banklist = await self.config.guildbanks()
for guild_id in banklist: for guild_id in banklist:
guild = self.bot.get_guild(guild_id) guild = self.bot.get_guild(guild_id)
if len(guild.emojis)<50: if len(guild.emojis) < 50:
guildbank = guild guildbank = guild
break break
if guildbank is None: if guildbank is None:
print("No guildbank to store emoji") print("No guildbank to store emoji")
# Eventually make a new banklist # Eventually make a new banklist
return return
# Next, have I saved this emoji before (because uploaded emoji != orignal emoji) # Next, have I saved this emoji before (because uploaded emoji != orignal emoji)
stolemojis = await self.config.stolemoji() stolemojis = await self.config.stolemoji()
if emoji.id in stolemojis: if emoji.id in stolemojis:
print("Emoji has already been stolen") print("Emoji has already been stolen")
return return
# Alright, time to steal it for real # Alright, time to steal it for real
# path = urlparse(emoji.url).path # path = urlparse(emoji.url).path
# ext = os.path.splitext(path)[1] # ext = os.path.splitext(path)[1]
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
img = await fetch_img(session, emoji.url) img = await fetch_img(session, emoji.url)
# path = data_manager.cog_data_path(cog_instance=self) / (emoji.name+ext) # path = data_manager.cog_data_path(cog_instance=self) / (emoji.name+ext)
# with path.open("wb") as f: # with path.open("wb") as f:
# f.write(img) # f.write(img)
# urllib.urlretrieve(emoji.url, emoji.name+ext) # urllib.urlretrieve(emoji.url, emoji.name+ext)
try: try:
await guildbank.create_custom_emoji(name=emoji.name,image=img,reason="Stole from "+str(user)) await guildbank.create_custom_emoji(name=emoji.name, image=img, reason="Stole from " + str(user))
except Forbidden as e: except discord.Forbidden as e:
print("PermissionError - no permission to add emojis") print("PermissionError - no permission to add emojis")
raise PermissionError("No permission to add emojis") from e raise PermissionError("No permission to add emojis") from e
except HTTPException: except discord.HTTPException as e:
print("HTTPException exception") print("HTTPException exception")
raise HTTPException # Unhandled error raise e # Unhandled error
# If you get this far, YOU DID IT # If you get this far, YOU DID IT
save_dict = self.default_stolemoji.copy() save_dict = self.default_stolemoji.copy()
e_dict = vars(emoji) e_dict = vars(emoji)
for k in e_dict: for k in e_dict:
if k in save_dict: if k in save_dict:
save_dict[k] = e_dict[k] save_dict[k] = e_dict[k]
save_dict["guildbank"] = guildbank.id save_dict["guildbank"] = guildbank.id
async with self.config.stolemoji() as stolemoji: async with self.config.stolemoji() as stolemoji:
stolemoji[emoji.id] = save_dict stolemoji[emoji.id] = save_dict
#Enable the below if you want to get notified when it works # Enable the below if you want to get notified when it works
# owner = await self.bot.application_info() # owner = await self.bot.application_info()
# owner = owner.owner # owner = owner.owner
# await owner.send("Just added emoji "+str(emoji)+" to server "+str(guildbank)) # await owner.send("Just added emoji "+str(emoji)+" to server "+str(guildbank))

Loading…
Cancel
Save