D
D
Daniel_Nine 1337pro82021-07-07 09:46:27
Python
Daniel_Nine 1337pro8, 2021-07-07 09:46:27

I get an error when typing a command to send a random media file from a folder, what should I do?

(I am using Python 3.9.5) here is the code:

import os
import random
import sys
import time
from os import listdir, path
from os import system
import discord
from discord.ext import commands
from progress.bar import IncrementalBar

client = commands.Bot(command_prefix='!')
token = open('token.txt', 'r').readline()

@client.command(pass_context=True)
async def Imgs(ctx, *args, **kwargs):
    author = ctx.message.author
    await ctx.send(file=discord.File(random.choice(os.listdir('images/'))))
    print('User ' + f'{author} use command: !Imgs')
client.run(token)

and the error code when entering the command:
  • Ignoring exception in command Imgs:
  • Traceback (most recent call last):
  • File "C:\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, wrapped
  • ret = await coro(*args, **kwargs)
  • File "C:\Users\1337pro8\Desktop\discord-bot\PTB\bot02.py", line 74, in Imgs
  • await ctx.send(file=discord.File(random.choice(os.listdir('images/')))) # sends any picture from folder on PC to discord chat
  • File "C:\Python39\lib\site-packages\discord\file.py", line 73, in __init__
  • self.fp = open(fp, 'rb')
  • FileNotFoundError: [Errno 2] No such file or directory: 'Screenshot_20210701-222444_Instagram.jpg'
  • The above exception was the direct cause of the following exception:
  • Traceback (most recent call last):
  • File "C:\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
  • await ctx.command.invoke(ctx)
  • File "C:\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
  • await injected(*ctx.args, **ctx.kwargs)
  • File "C:\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, wrapped
  • raise CommandInvokeError(exc) from exc
  • discord.ext.commands.errors.CommandInvokeError: Command raised an exception: FileNotFoundError: [Errno 2] No such file or directory: 'Screenshot_20210701-222444_Instagram.jpg'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-07-07
@1337pro8

exc is a required argument that is missing

Don't pass exc to a function. Either add it after the command, !Imgs 123or remove it from the function altogether if it is not used

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question