首页 > 解决方案 > Discord.py 重写:函数突然停止工作

问题描述

在过去的几天里,我一直在搞乱 discord bot python rewrite。我注意到添加多个功能往往会导致其他功能停止工作。我添加了一个循环任务,它从随机的 URL 列表中发送视频,我最初认为这可能会导致其他功能停止工作,但将其注释掉仍然会导致问题。当您没有错误消息要关闭时,这很烦人。任何帮助将不胜感激!

import discord
from discord.ext import commands, tasks
import re
from random import randint

client = commands.Bot(command_prefix = "//")

@client.event
async def on_ready():
    print("The bot is doing stuff.")


@client.event
async def on_message(message):
    """Deletes <specific> YT link"""

    if <specific yt link> in message.content: 
        await message.delete()
        print("video deleted")


@client.event
async def on_message(message):
    "Sends a peeposalute whenever someone says 'goodnight'"

    gn = re.compile('goodnight|good night|gn')
    if gn.search(message.content.lower()): 
        await message.channel.send('<:peepoSalute:665687773407215637>')
        print('peepo has saluted')


@client.event
async def on_message(message):
    """Checks the #new-uploads channel, reacts to any new messages and adds the video URL to the list"""

    if message.channel.id == <channel id>:
        with open('videos.txt', 'a') as f:
            f.write(message.content + '\n')
            print("New video added to list")
        message.add_reaction('<:Wowee:592122719546638408>')


@tasks.loop(hours=24)
async def daily_video():
    """Sends a random video in #general daily"""

    with open('videos.txt', 'r') as f:
        videos = f.readlines()

    target_general_id = <channel id>
    general_id = client.get_channel(target_general_id)
    await general_id.send(videos[randint(0, len(videos) - 1)])
    print("Daily video sent")

@daily_video.before_loop
async def before():
    await client.wait_until_ready()
    print("Finished waiting")

daily_video.start()

client.run(<token>)

标签: python-3.xdiscord.py

解决方案


推荐阅读