discord.py一次与for循环一起发送多个文件

发布于 2025-02-12 20:53:01 字数 552 浏览 1 评论 0原文

因此,我正在尝试制作一个不和谐的

机器发送图像

import discord
import os

client = discord.Client()

@client.event
async def on_ready():
  print('Logged in as')
  print(client.user.name)
  print(client.user.id)
  print('------')


@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$doit'):
        await message.channel.send(file=discord.File("./images/*"))

client.run("OT*****************************************")

So I'm trying to make a discord bot that when I write $doit it sends all the files in the image folder but I would like it to do it one by one

So basically make a for loop and send the images

import discord
import os

client = discord.Client()

@client.event
async def on_ready():
  print('Logged in as')
  print(client.user.name)
  print(client.user.id)
  print('------')


@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$doit'):
        await message.channel.send(file=discord.File("./images/*"))

client.run("OT*****************************************")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

向日葵 2025-02-19 20:53:01

这是我的代码正常:

import discord
from discord.ext import commands
import os

bot = commands.Bot(command_prefix="$")

@bot.event
async def on_ready():
  print('Logged in as')
  print(bot.user.name)
  print(bot.user.id)
  print('------')

@bot.command()
async def doit(ctx):
    dir_path = r"your directory path here"
    res = []  # empty list to be filled up with your files names

    for path in os.listdir(dir_path):
        if os.path.isfile(os.path.join(dir_path, path)):
            res.append(path)

    for r in res:
        with open(dir_path + "\\" + r, 'rb') as f:
            picture = discord.File(f)
            await ctx.channel.send(file=picture)


bot.run("your token here")

我做了什么:

  1. 我制作了$ doit命令,因为它是命令,而不是事件。我将您的前缀定义为$doit为命令/函数。
  2. 我已经使用OS库列出了目录中的所有文件,您可以将其定义为dir_path actible,然后将文件名添加到res list 。
  3. 就像您说的那样,我为循环做了一个,将每个文件从res列表发送为单独的消息。

它对我来说很好。

如果您有任何疑问,请随时提出。 :)

ps现在它将目录中的每个文件发送,无论是否是图像。您可以将其限制为仅使用简单的图像,如果条件并指定文件的扩展(.jpg.png等) 。

Here's my code that works fine:

import discord
from discord.ext import commands
import os

bot = commands.Bot(command_prefix="
quot;)

@bot.event
async def on_ready():
  print('Logged in as')
  print(bot.user.name)
  print(bot.user.id)
  print('------')

@bot.command()
async def doit(ctx):
    dir_path = r"your directory path here"
    res = []  # empty list to be filled up with your files names

    for path in os.listdir(dir_path):
        if os.path.isfile(os.path.join(dir_path, path)):
            res.append(path)

    for r in res:
        with open(dir_path + "\\" + r, 'rb') as f:
            picture = discord.File(f)
            await ctx.channel.send(file=picture)


bot.run("your token here")

What I've done:

  1. I've made $doit a command, since it's rather a command, not an event. I've defined your prefix as $ and doit as the command/function.
  2. I've used os library to list all the files in the directory you can define as dir_path variable and add the files names to the res list.
  3. I've made a for loop just as you said to send every file from res list as a separate message.

It worked fine for me.

If you have any questions, do not hesitate to ask. :)

P.S. Now it sends every file in a directory, no matter if it is an image or not. You can limit it to just images using a simple if condition and specify the extensions of the files (.jpg, .png, etc.).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文