【问题标题】:Discord.py random image to send to ctx.message.channelDiscord.py 随机图像发送到 ctx.message.channel
【发布时间】:2020-07-12 02:15:50
【问题描述】:

所以我正在尝试制作一小段代码 从文件夹发送随机图像 我正在使用 discord.py 我不知道怎么做 所以我认为stackoverflow可以帮助我! 对不起,伙计们,但我实际上对 python 编码有点陌生 我真的很烂

【问题讨论】:

  • 您好,这是一个提供编码帮助而不是编写代码的网站。到目前为止,您尝试过什么?

标签: python-3.x random discord.py-rewrite


【解决方案1】:

使用您使用的 discord.py 发送图像

await channel.send(file=discord.File('my_file.png'))

如果您想发送随机图像,您可以将每个文件名保存到一个列表中,然后在命令中从该列表中发送一个随机文件:

import random

files = ['img1.png', 'img2.png', 'img3.png']

@bot.command()
async def image(ctx):
    file = random.choice(files)
    await channel.send(file=discord.File(file))

如果您想自动将每个图像文件添加到与运行机器人的 .py 文件相同的目录中,您可以使用os module

import os

files = []
for file in os.listdir():
    if file.endswith('.png'):
        files.append(file)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2020-05-13
    • 2023-03-09
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    相关资源
    最近更新 更多