【问题标题】:Discord Bot Image Scraper using Praw - ErrorDiscord Bot Image Scraper 使用 Praw - 错误
【发布时间】:2021-12-12 05:56:21
【问题描述】:

我正在尝试编写一个机器人,它根据给定的命令从 r/horses 生成马的随机图片。我不知何故走到了死胡同——我的代码似乎完好无损,但它不会在我的服务器中生成我的马图片。我不知道为什么!任何帮助将不胜感激。

这是我的代码:

from typing import Any, Iterator

TOKEN = 'token'

from discord.ext import commands
import random
import praw


client = commands.Bot(description="horse", command_prefix=None)
reddit = praw.Reddit(client_id='id',
                     client_secret='secret',
                     user_agent='horse bot')
@client.command()
async def horse(ctx):
    horses_submissions = reddit.subreddit('horses').hot(limit=100)
    horses_submissions = [x for x in horses_submissions if not x.stickied]
    submission = random.choice(horses_submissions)
    await ctx.send(submission.url)

@client.event
async def on_ready():
    print('the ranch! the ranch! {0.user} has joined the ranch!'.format(client))

@client.event
async def on_message(message):
    username = str(message.author).split('#')[0]
    user_message = str(message.content)
    channel = str(message.channel.name)
    print(f'{username}: {user_message} ({channel})')

    if message.author == client.user:
        return
    if message.channel.name == 'the-ranch':
        if user_message.lower() == 'hello':
            await message.channel.send(f'greetings {username}! give me oats')
            return
        elif user_message.lower() == 'bye':
            await message.channel.send(f'please bring oats when you return, {username}..')
            return
        elif user_message.lower() == "!random":
            response = f' Here, have a random number, (as a treat): {random.randrange(1000000)}'
            await message.channel.send(response)
            return

    if user_message.lower() == '!anywhere':
        await message.channel.send('hey i am here too')

    #pics
    if message.content.startswith('horse'):
        horses = await horse()
        await message.channel.send(horses)


client.run(TOKEN)

这是我收到的错误:

C:\Users\17734\bot\venv\Scripts\python.exe C:/Users/17734/bot/main.py
the ranch! the ranch! horse bot#5576 has joined the ranch!
aggie: horse (the-ranch)
Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\17734\bot\venv\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\17734\bot\main.py", line 51, in on_message
    horses = await horse()
  File "C:\Users\17734\bot\venv\lib\site-packages\discord\ext\commands\core.py", line 374, in __call__
    return await self.callback(*args, **kwargs)
TypeError: horse() missing 1 required positional argument: 'ctx'

感谢您的帮助!

最后编辑于 21 年 10 月 26 日下午 5:11 CST

【问题讨论】:

    标签: python image web-scraping discord praw


    【解决方案1】:

    首先,考虑使用async praw,我建议将它与 Discord 机器人一起使用。参考https://asyncpraw.readthedocs.io/en/stable/了解一下。

    问题可能是您获得了一个介于 1 和 1000 之间的随机数,但您只从 Reddit API 收到前 100 个热门图像。尝试在第 17 行之前添加 print('index: {i}'.format(i=i)),以便调试代码并查看是否是 Index Out Of Range 错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-05
      • 2019-11-10
      • 2018-05-02
      • 2021-07-15
      • 2019-02-03
      • 2018-03-12
      • 2018-03-04
      相关资源
      最近更新 更多