【发布时间】: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