<gh_stars>0
import discord
from discord.ext import commands
import asyncio
from utils import permissions, database
from utils import databasewrapper as db
from tinydb import Query

Q = Query()

reactions = ['\U00000031\U000020e3', '\U00000032\U000020e3', '\U00000033\U000020e3', '\U00000034\U000020e3', '\U00000035\U000020e3']
valid_statuses = ['pending', 'considered', 'implemented', 'rejected']

class Feedback(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    @commands.guild_only()
    @permissions.helper_only()
    async def rate(self, ctx, member:discord.Member):
        """Sends a support rating form to an user"""

        try:
            member_msg = await member.send(f'Hey there! You recently asked for support in **{ctx.guild.name}**, and we would like to know how good was the help you received. Please rate the support with the reactions below (1 worst, 5 best)')

            for r in reactions:
                await member_msg.add_reaction(r)

            await ctx.message.add_reaction('\u2705')

            try:
                def check(r, u):
                    return str(r.emoji) in reactions and u.id == member.id and r.message.id == member_msg.id

                reaction, user = await self.bot.wait_for('reaction_add', timeout=3600.0, check=check)
            except asyncio.TimeoutError:
                await ctx.send(f':warning: {ctx.author.mention}, your rate request to {str(member)} (`{member.id}`) failed due to timeout')
                await member_msg.edit(content=':x: You took too long to answer, this form will not work anymore')

            else:
                await ctx.send(f':white_check_mark: {ctx.author.mention}, your rate request to {str(member)} (`{member.id}`) has been successful. Rating given was: {str(reaction.emoji)}')
                await member_msg.edit(content=f'You rated: {str(reaction.emoji)}. Thank you for the feedback!')
            finally:
                for r in reactions:
                    await member_msg.remove_reaction(r, ctx.guild.me)

        except discord.Forbidden:
            ctx.send(f':x: Could not send {str(member.name)}(`{member.id}`) a DM')

    @commands.group(invoke_without_command=True)
    @commands.guild_only()
    async def feedback(self, ctx, *, content):
        """Sends feedback to the server Staff"""

        gdata = await db.get_guild(ctx.guild)

        embed = discord.Embed(description=content, color=discord.Color(0xCACDCD))

        embed.set_author(name=str(ctx.author), icon_url=ctx.author.avatar_url)

        id = database.tables['feedback'].insert(database.default_feedback_data(ctx.guild, ctx.author, ctx.message))

        embed.set_footer(text=f'Feedback ID: {id} | Author ID: {ctx.author.id}')

        try:
            feedback_channel = self.bot.get_channel(gdata.get('feedbackchannel', None))

            if feedback_channel:
                msg = await feedback_channel.send(embed=embed)

                database.tables['feedback'].update({'message': msg.id}, doc_ids=[id])
        except Exception as e:
            print(e)
            await ctx.send(':x: Feedback channel does not exist, is misconfigured or I do not have permissions to send messages in it. Please contact a staff member')
        else:
            await ctx.send(':white_check_mark: Your feedback has been submitted!')

    @feedback.command(name='completed')
    @permissions.trial_mod_only()
    async def feedback_complete(self, ctx, id:int):
        fdata = await db.get_feedback(id)
        gdata = await db.get_guild(ctx.guild)

        if not fdata:
            return await ctx.send(':x: Unknown post ID')

        feedback_channel = self.bot.get_channel(gdata.get('feedbackchannel', None))

        if not feedback_channel:
            await ctx.send(':x: Feedback channel does not exist, is misconfigured or I do not have permissions to send messages in it. Please contact a staff member')

        try:
            msg = await feedback_channel.fetch_message(fdata['message'])
            author = await self.bot.fetch_user(fdata['author'])

            embed = msg.embeds[0]

            embed.color = discord.Color(0x7ED321)
            embed.title = 'Feedback completed!'

            embed.set_author(name=str(author), icon_url=author.avatar_url)

            await msg.edit(embed=embed)


            fdata.status = 'completed'

            await db.update_feedback_back([fdata])

            await author.send(f':grey_exclamation: Hey there! Your feedback `#{id}` has been marked as completed! \n https://discordapp.com/channels/{ctx.guild.id}/{feedback_channel.id}/fdata["message"]')

        except Exception as e:
            raise
        else:
            await ctx.send(f':white_check_mark: Feedback `#{fdata.doc_id}` has been set as completed')

    @feedback.command(name='considered')
    @permissions.trial_mod_only()
    async def feedback_consider(self, ctx, id:int):
        fdata = await db.get_feedback(id)
        gdata = await db.get_guild(ctx.guild)

        if not fdata:
            return await ctx.send(':x: Unknown post ID')

        feedback_channel = self.bot.get_channel(gdata.get('feedbackchannel', None))

        if not feedback_channel:
            await ctx.send(':x: Feedback channel does not exist, is misconfigured or I do not have permissions to send messages in it. Please contact a staff member')

        try:
            msg = await feedback_channel.fetch_message(fdata['message'])
            author = await self.bot.fetch_user(fdata['author'])

            embed = msg.embeds[0]

            embed.color = discord.Color(0x50E3C2)
            embed.title = 'Feedback considered!'

            embed.set_author(name=str(author), icon_url=author.avatar_url)

            await msg.edit(embed=embed)

            fdata.status = 'considered'

            await db.update_feedback_back([fdata])

        except Exception as e:
            raise
        else:
            await ctx.send(f':white_check_mark: Feedback `#{fdata.doc_id}` has been set as considered')

    @feedback.command(name='rejected')
    @permissions.trial_mod_only()
    async def feedback_reject(self, ctx, id:int):
        fdata = await db.get_feedback(id)
        gdata = await db.get_guild(ctx.guild)

        if not fdata:
            return await ctx.send(':x: Unknown post ID')

        feedback_channel = self.bot.get_channel(gdata.get('feedbackchannel', None))

        if not feedback_channel:
            await ctx.send(':x: Feedback channel does not exist, is misconfigured or I do not have permissions to send messages in it. Please contact a staff member')

        try:
            msg = await feedback_channel.fetch_message(fdata['message'])
            author = await self.bot.fetch_user(fdata['author'])

            embed = msg.embeds[0]

            embed.color = discord.Color(0xA4071C)
            embed.title = 'Feedback rejected'

            embed.set_author(name=str(author), icon_url=author.avatar_url)

            await msg.edit(embed=embed)

            fdata.status = 'rejected'

            await db.update_feedback_back([fdata])

        except Exception as e:
            raise
        else:
            await ctx.send(f':white_check_mark: Feedback `#{fdata.doc_id}` has been set as rejected')


    @feedback.command(name='comment')
    @permissions.trial_mod_only()
    async def feedback_comment(self, ctx, id:int, *, content):

        fdata = await db.get_feedback(id)
        gdata = await db.get_guild(ctx.guild)



        if not fdata:
            return await ctx.send(':x: Unknown post ID')

        feedback_channel = self.bot.get_channel(gdata.get('feedbackchannel', None))

        if not feedback_channel:
            await ctx.send(':x: Feedback channel does not exist, is misconfigured or I do not have permissions to send messages in it. Please contact a staff member')

        try:
            msg = await feedback_channel.fetch_message(fdata['message'])
            author = await self.bot.fetch_user(fdata['author'])

            embed = msg.embeds[0]

            embed.set_author(name=str(author), icon_url=author.avatar_url)

            embed.add_field(
                name= f':speech_balloon: {str(ctx.author)} commented:',
                value= content,
                inline= False
            )

            if len(embed) > 6000 or len(embed.fields) > 25:
                return await ctx.send(':x: You cannot add your comment because it would go over the character limit')

            await msg.edit(embed=embed)

            fdata['comments'][ctx.message.id] = {
                'id': ctx.message.id,
                'author': ctx.author.id,
                'content': content
            }

            await db.update_feedback_back([fdata])

        except Exception as e:
            raise
        else:
            await ctx.send(f':white_check_mark: Your comment on feedback `#{fdata.doc_id}` has been added')



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

import discord
from discord.ext import commands
import asyncio
from utils import permissions, database
from utils import databasewrapper as db
from tinydb import Query

Q = Query()

reactions = ['\U00000031\U000020e3', '\U00000032\U000020e3', '\U00000033\U000020e3', '\U00000034\U000020e3', '\U00000035\U000020e3']
valid_statuses = ['pending', 'considered', 'implemented', 'rejected']

class Feedback(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    @commands.guild_only()
    @permissions.helper_only()
    async def rate(self, ctx, member:discord.Member):
        """Sends a support rating form to an user"""

        try:
            member_msg = await member.send(f'Hey there! You recently asked for support in **{ctx.guild.name}**, and we would like to know how good was the help you received. Please rate the support with the reactions below (1 worst, 5 best)')

            for r in reactions:
                await member_msg.add_reaction(r)

            await ctx.message.add_reaction('\u2705')

            try:
                def check(r, u):
                    return str(r.emoji) in reactions and u.id == member.id and r.message.id == member_msg.id

                reaction, user = await self.bot.wait_for('reaction_add', timeout=3600.0, check=check)
            except asyncio.TimeoutError:
                await ctx.send(f':warning: {ctx.author.mention}, your rate request to {str(member)} (`{member.id}`) failed due to timeout')
                await member_msg.edit(content=':x: You took too long to answer, this form will not work anymore')

            else:
                await ctx.send(f':white_check_mark: {ctx.author.mention}, your rate request to {str(member)} (`{member.id}`) has been successful. Rating given was: {str(reaction.emoji)}')
                await member_msg.edit(content=f'You rated: {str(reaction.emoji)}. Thank you for the feedback!')
            finally:
                for r in reactions:
                    await member_msg.remove_reaction(r, ctx.guild.me)

        except discord.Forbidden:
            ctx.send(f':x: Could not send {str(member.name)}(`{member.id}`) a DM')

    @commands.group(invoke_without_command=True)
    @commands.guild_only()
    async def feedback(self, ctx, *, content):
        """Sends feedback to the server Staff"""

        gdata = await db.get_guild(ctx.guild)

        embed = discord.Embed(description=content, color=discord.Color(0xCACDCD))

        embed.set_author(name=str(ctx.author), icon_url=ctx.author.avatar_url)

        id = database.tables['feedback'].insert(database.default_feedback_data(ctx.guild, ctx.author, ctx.message))

        embed.set_footer(text=f'Feedback ID: {id} | Author ID: {ctx.author.id}')

        try:
            feedback_channel = self.bot.get_channel(gdata.get('feedbackchannel', None))

            if feedback_channel:
                msg = await feedback_channel.send(embed=embed)

                database.tables['feedback'].update({'message': msg.id}, doc_ids=[id])
        except Exception as e:
            print(e)
            await ctx.send(':x: Feedback channel does not exist, is misconfigured or I do not have permissions to send messages in it. Please contact a staff member')
        else:
            await ctx.send(':white_check_mark: Your feedback has been submitted!')

    @feedback.command(name='completed')
    @permissions.trial_mod_only()
    async def feedback_complete(self, ctx, id:int):
        fdata = await db.get_feedback(id)
        gdata = await db.get_guild(ctx.guild)

        if not fdata:
            return await ctx.send(':x: Unknown post ID')

        feedback_channel = self.bot.get_channel(gdata.get('feedbackchannel', None))

        if not feedback_channel:
            await ctx.send(':x: Feedback channel does not exist, is misconfigured or I do not have permissions to send messages in it. Please contact a staff member')

        try:
            msg = await feedback_channel.fetch_message(fdata['message'])
            author = await self.bot.fetch_user(fdata['author'])

            embed = msg.embeds[0]

            embed.color = discord.Color(0x7ED321)
            embed.title = 'Feedback completed!'

            embed.set_author(name=str(author), icon_url=author.avatar_url)

            await msg.edit(embed=embed)


            fdata.status = 'completed'

            await db.update_feedback_back([fdata])

            await author.send(f':grey_exclamation: Hey there! Your feedback `#{id}` has been marked as completed! \n https://discordapp.com/channels/{ctx.guild.id}/{feedback_channel.id}/fdata["message"]')

        except Exception as e:
            raise
        else:
            await ctx.send(f':white_check_mark: Feedback `#{fdata.doc_id}` has been set as completed')

    @feedback.command(name='considered')
    @permissions.trial_mod_only()
    async def feedback_consider(self, ctx, id:int):
        fdata = await db.get_feedback(id)
        gdata = await db.get_guild(ctx.guild)

        if not fdata:
            return await ctx.send(':x: Unknown post ID')

        feedback_channel = self.bot.get_channel(gdata.get('feedbackchannel', None))

        if not feedback_channel:
            await ctx.send(':x: Feedback channel does not exist, is misconfigured or I do not have permissions to send messages in it. Please contact a staff member')

        try:
            msg = await feedback_channel.fetch_message(fdata['message'])
            author = await self.bot.fetch_user(fdata['author'])

            embed = msg.embeds[0]

            embed.color = discord.Color(0x50E3C2)
            embed.title = 'Feedback considered!'

            embed.set_author(name=str(author), icon_url=author.avatar_url)

            await msg.edit(embed=embed)

            fdata.status = 'considered'

            await db.update_feedback_back([fdata])

        except Exception as e:
            raise
        else:
            await ctx.send(f':white_check_mark: Feedback `#{fdata.doc_id}` has been set as considered')

    @feedback.command(name='rejected')
    @permissions.trial_mod_only()
    async def feedback_reject(self, ctx, id:int):
        fdata = await db.get_feedback(id)
        gdata = await db.get_guild(ctx.guild)

        if not fdata:
            return await ctx.send(':x: Unknown post ID')

        feedback_channel = self.bot.get_channel(gdata.get('feedbackchannel', None))

        if not feedback_channel:
            await ctx.send(':x: Feedback channel does not exist, is misconfigured or I do not have permissions to send messages in it. Please contact a staff member')

        try:
            msg = await feedback_channel.fetch_message(fdata['message'])
            author = await self.bot.fetch_user(fdata['author'])

            embed = msg.embeds[0]

            embed.color = discord.Color(0xA4071C)
            embed.title = 'Feedback rejected'

            embed.set_author(name=str(author), icon_url=author.avatar_url)

            await msg.edit(embed=embed)

            fdata.status = 'rejected'

            await db.update_feedback_back([fdata])

        except Exception as e:
            raise
        else:
            await ctx.send(f':white_check_mark: Feedback `#{fdata.doc_id}` has been set as rejected')


    @feedback.command(name='comment')
    @permissions.trial_mod_only()
    async def feedback_comment(self, ctx, id:int, *, content):

        fdata = await db.get_feedback(id)
        gdata = await db.get_guild(ctx.guild)



        if not fdata:
            return await ctx.send(':x: Unknown post ID')

        feedback_channel = self.bot.get_channel(gdata.get('feedbackchannel', None))

        if not feedback_channel:
            await ctx.send(':x: Feedback channel does not exist, is misconfigured or I do not have permissions to send messages in it. Please contact a staff member')

        try:
            msg = await feedback_channel.fetch_message(fdata['message'])
            author = await self.bot.fetch_user(fdata['author'])

            embed = msg.embeds[0]

            embed.set_author(name=str(author), icon_url=author.avatar_url)

            embed.add_field(
                name= f':speech_balloon: {str(ctx.author)} commented:',
                value= content,
                inline= False
            )

            if len(embed) > 6000 or len(embed.fields) > 25:
                return await ctx.send(':x: You cannot add your comment because it would go over the character limit')

            await msg.edit(embed=embed)

            fdata['comments'][ctx.message.id] = {
                'id': ctx.message.id,
                'author': ctx.author.id,
                'content': content
            }

            await db.update_feedback_back([fdata])

        except Exception as e:
            raise
        else:
            await ctx.send(f':white_check_mark: Your comment on feedback `#{fdata.doc_id}` has been added')



def setup(bot):
    bot.add_cog(Feedback(bot))
Sends a support rating form to an user Sends feedback to the server Staff #{id}` has been marked as completed! \n https://discordapp.com/channels/{ctx.guild.id}/{feedback_channel.id}/fdata["message"]') #{fdata.doc_id}` has been set as completed') #{fdata.doc_id}` has been set as considered') #{fdata.doc_id}` has been set as rejected') #{fdata.doc_id}` has been added')