python制作reddit机器人的新手

2024-06-26 14:40:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个reddit discord机器人,我不知道我做错了什么

import discord
from discord.ext import commands
import praw
reddit = praw.Reddit(client_id = "sda",
                     client_secret = "fdsf",
                     username = "aasdsa",
                     password = "password",
                     user_agent = "sd")

b = commands.Bot(command_prefix = ".p ")

@b.command()

async def meme(ctx):

    subreddit = reddit.subreddit("dankmemes")
    all_subs = []

    top = subreddit.top(limit = 10)

    for sumbission in top:
        all_subs.append(sumbission)

    random_sub = all_subs.choice()

    name = random_sub.title()
    url - random_sub.url()

    em = discord.Embed(title = name)
    
    em.set_image(url = url)

    await ctx.send(embed = em)

每当我键入.p sub时,它都应该从subreddit发送一篇文章。p sub,有什么问题,为什么它不工作?我还在另一个.py文件中尝试了这个方法,如果不立即关闭,该文件将无法打开。另外,我应该在user_agent中键入什么?如果您需要,这是我的应用程序:https://ibb.co/B64vrWM

import praw

reddit = praw.Reddit(client_id = "example",
                     client_secret = "example",
                     username = "Psychological_Win_55",
                     password = "not_my_real_password",
                     user_agent = "I don't know what I put here")

subreddit = reddit.subreddit("memes")
all_subs = []

top = subreddit.hot(limit=10)

for sumbission in subreddit.hot(limit=10):
    print(sumbission.title)

Tags: importclienturltoppasswordallagentsubs
2条回答

所以我想出来了

当我告诉python选择一个随机提交时,我需要这样做

import random

然后呢

random.choice(all_subs)

而不是

random_sub = all_subs.choice()

对我来说,这似乎是一个根python问题,因为您声称较低代码块中的python文件似乎没有运行。它要么打印10个提交标题(praw语法正确),要么抛出某种错误(403或404)

因为这些都没有发生,所以我猜您可能是Python新手,而且我还猜您在Windows上

在本例中,假设安装了praw(if not, install it with pip),我建议转到Powershell(而不是python shell),并输入:

python3 path/to/file.py

(可以是python而不是python3,具体取决于您当前的配置

当你运行它时,你要么会得到一个有助于调试的错误,要么会打印出10个提交标题

如果我的任何假设是错误的,请让我知道

相关问题 更多 >