0
votes

Hi I'm a bit stuck on a syntax error I'm not sure if it's a indent error or incorrect usage of except discord.NotFound

This is the error I get in the console

File "C:\Users\Tom\Documents\adiscordhelper\cogs\star.py", line 119
except discord.NotFound:
     ^
SyntaxError: invalid syntax

This is how I'm using the exception and wanting to handle the error in my code, here is snippet.

 @commands.command(pass_context=True)
@checks.mod_or_permissions()
async def star(self, ctx, msg_id):
    """Manually add message to the starboard follows same restrictions
       does not have a threshold"""
    server = ctx.message.server
    channel = ctx.message.channel
    user = discord.utils.get(server.members)
    msg = await self.bot.get_message(channel, msg_id)
    author = msg.author
    if server.id not in self.settings:
        return
    if msg.channel.id in self.settings[server.id]["ignore"]:
        return

    emoji =self.settings[server.id]["emoji"]
    threshold = self.settings[server.id]["threshold"]
    count = await self.get_count(server, msg)
    if await self.check_is_posted(server, msg):
        channel = self.bot.get_channel(self.settings[server.id]["channel"])
        msg_id, count = await self.get_posted_message(server, msg)
        if msg_id is not None:
            msg_edit = await self.bot.get_message(channel, msg_id)
            await self.bot.edit_message(new_content="{} your post made it to the star **{}** board! :raised_hands:".format(author.mention, emoji))
            return

    # else:
    channel2 = self.bot.get_channel(id=self.settings[server.id]["channel"])
    em = await self.build_embed(msg)
    post_msg = await self.bot.send_message(channel2, "{} your post made it to the {} board! :raised_hands:".format(author.mention, emoji, 1), embed=em)
    modpush = "{} added {}'s post to {} :star2:".format(user.mention, author.name, channel2.mention)
    await self.bot.send_message(channel, modpush)
    await self.bot.delete_message(ctx.message)
except discord.NotFound:
    await self.bot.send_message("Message not found.")
    past_message_list = self.settings[server.id]["messages"]
    past_message_list.append({"original_message":msg.id, "new_message":msg.id, "count":1})
    dataIO.save_json("data/star/settings.json", self.settings)

I'm not sure where I'm going wrong here so your help would be appreciated.

Edit adding try statement:

channel2 = self.bot.get_channel(id=self.settings[server.id]["channel"])
    em = await self.build_embed(msg)
    post_msg = await self.bot.send_message(channel2, "{} your post made it to the {} board! :raised_hands:".format(author.mention, emoji, 1), embed=em)
try:
    modpush = "{} added {}'s post to {} :star2:".format(user.mention, author.name, channel2.mention)
    await self.bot.send_message(channel, modpush)
    await self.bot.delete_message(ctx.message)
except discord.NotFound:
    await self.bot.send_message("Message not found.")
    past_message_list = self.settings[server.id]["messages"]
    past_message_list.append({"original_message":msg.id, "new_message":msg.id, "count":1})
    dataIO.save_json("data/star/settings.json", self.settings)
2
@patrick Haugh Hi sorry I'm still struggling here I've added try state see edit in original post but I'm still getting \cogs\star.py", line 118 await self.bot.send_message(channel, modpush) ^ SyntaxError: invalid syntax - onnex
What version of python are you using? - Patrick Haugh
@PatrickHaugh python-3.6.6 - onnex
It's probably an indentation issue then. - Patrick Haugh

2 Answers

1
votes

The issue is because the try statement is in the wrong place.

It should look like this:

 @commands.command(pass_context=True)
@checks.mod_or_permissions()
async def star(self, ctx, msg_id):
    """Manually add message to the starboard follows same restrictions
       does not have a threshold"""
    try:
        server = ctx.message.server
        channel = ctx.message.channel
        user = discord.utils.get(server.members)
        msg = await self.bot.get_message(channel, msg_id)
        author = msg.author
        if server.id not in self.settings:
            return
        if msg.channel.id in self.settings[server.id]["ignore"]:
            return

        emoji =self.settings[server.id]["emoji"]
        threshold = self.settings[server.id]["threshold"]
        count = await self.get_count(server, msg)
        if await self.check_is_posted(server, msg):
            channel = self.bot.get_channel(self.settings[server.id]["channel"])
            msg_id, count = await self.get_posted_message(server, msg)
            if msg_id is not None:
                msg_edit = await self.bot.get_message(channel, msg_id)
                await self.bot.edit_message(new_content="{} your post made it to the star **{}** board! :raised_hands:".format(author.mention, emoji))

    # else:
        channel2 = self.bot.get_channel(id=self.settings[server.id]["channel"])
        em = await self.build_embed(msg)    
        post_msg = await self.bot.send_message(channel2, "{} your post made it to the {} board! :raised_hands:".format(author.mention, emoji, 1), embed=em)
        await self.bot.send_message(channel, "{} added {}'s post to {} :star2:".format(user.mention, author.name, channel2.mention))
        await self.bot.delete_message(ctx.message)
        past_message_list = self.settings[server.id]["messages"]
        past_message_list.append({"original_message":msg.id, "new_message":msg.id, "count":1})
        dataIO.save_json("data/star/settings.json", self.settings)
    except discord.NotFound: 
        await self.bot.send_message("Message not found.")

You also need to delete the return statement after this line:

await self.bot.edit_message(new_content="{} your post made it to the star **{}** board! :raised_hands:".format(author.mention, emoji))
0
votes

The except block tells python what to do if the associated try block fails. You don't have a try block, so it's throwing a syntax error.