python 制作 reddit 机器人的新手

New to python making a reddit bot

我正在制作一个 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 发送一个 post,这是怎么回事,为什么它不起作用?我还在另一个 .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)

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

由于发生了 none,我猜你可能是 Python 的新手,我也猜你在 Windows.

在这种情况下,假设安装了 praw (if not, install it with pip),我建议转到 Powershell(而不是 python shell),然后输入:

python3 path/to/file.py

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

当你运行那样做时,你要么得到一个有助于调试的错误,要么打印 10 个提交标题!

如果我的任何假设有误,请告诉我。

所以我想通了。

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

import random

然后

random.choice(all_subs)

而不是

random_sub = all_subs.choice()