Chezloc
Chezloc

Reputation: 1

message.attachments not working discord.py

I'm trying to code a command to download the attachment from discord and save it in my directory of choice on the server the bot is running but I get this error: https://i.sstatic.net/AyJFJ.png Does anyone have an idea why this is happening?

this is my code:

import discord
from discord.ext import commands

# ---CONFIG---
cmp_structure_path = '/root/servers/creative/server/world-cmp0/generated/minecraft/structures/'
# ---CONFIG---

# ping command 
class structure(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
    @commands.has_role(890473812917891092)
    @commands.command(help = 'Download and upload structure files to/from cmp')
    async def structure(self, ctx, strucaction, strucname=None):
        if strucaction == 'upload':
            if str(message.attachments) == "[]": # Checks if there is an attachment on the message
                return
            else: # If there is it gets the filename from message.attachments
                split_v1 = str(message.attachments).split("filename='")[1]
                filename = str(split_v1).split("' ")[0]
                if filename.endswith(".nbt"): # Checks if it is a .nbt file
                    await message.attachments.save(fp=cmp_structure_path.format(filename)) # saves the file
                    embed = discord.Embed(
                    title = 'Structure ' + filename + ' uploaded'
                    )
                    embed.set_footer(text='Chronos™'),
                    await ctx.send(embed=embed)
        elif strucaction == 'download':
            return
        else:
                embed = discord.Embed(
                title = 'Invalid Arguments'
                )
                embed.set_footer(text='Chronos™'),
                await ctx.send(embed=embed)

def setup(bot): # a extension must have a setup function
    bot.add_cog(structure(bot)) # adding a cog

Upvotes: 0

Views: 558

Answers (1)

Divan
Divan

Reputation: 58

The varaible message does not exist, write ctx.message or create a variable in the beginning of the structure function like so:

message = ctx.message

Upvotes: 1

Related Questions