【问题标题】:Why isnt my bot sending a message in discord py为什么我的机器人没有在 discord py 中发送消息
【发布时间】:2021-05-23 16:05:16
【问题描述】:

我的代码基本上是对当前时区进行条件语句。如果该时区条件中的时间为真,则它会发送一条消息来锁定角色。但是,当我将代码放入 discord 的 py 函数中时,我的代码不会发送消息

import os
import discord
from discord.ext import commands 
import datetime
import pytz


intents = discord.Intents.default() 
intents.members = True
client = commands.Bot(command_prefix = '?',intents=intents) #sets prefix 
client.remove_command('help')


f = 19
m = 0 
@client.event
async def on_ready():
    await client.change_presence(status=discord.Status.do_not_disturb, activity= discord.Activity(name="Around ;)", type=discord.ActivityType.watching))
    print('ready')

@client.event
async def time_check():
    await client.wait_until_ready()
    while not client.is_closed:
        cst = datetime.datetime.now(tz=pytz.timezone('US/Central')).time()
        if cst.hour == 19 and cst.minute == 35:
            channel = client.get_channel(760182149839716423)
            await channel.send("<@&811801850771800134>")
            await channel.send("<@&811808231218610266>")
            await channel.send("<@&811802000463101963>")
        elif cst.hour == 19 and cst.minute == 40:
            channel = client.get_channel(760182149839716423)
            await channel.send("<@&811801850771800134>")
            await channel.send("<@&811808231218610266>")
            await channel.send("<@&811802000463101963>")
        elif cst.hour == 9 and cst.minute == 00:
            channel = client.get_channel(760182149839716423)
            await channel.send("<@&811801850771800134>")
            await channel.send("<@&811808231218610266>")
            await channel.send("<@&811802000463101963>")
        elif cst.hour == 13 and cst.minute == 00:
            channel = client.get_channel(760182149839716423)
            await channel.send("<@&811801850771800134>")
            await channel.send("<@&811808231218610266>")
            await channel.send("<@&811802000463101963>")
            break
        elif cst.hour == 18 and cst.minute == 00:
            channel = client.get_channel(760182149839716423)
            await channel.send("<@&811801850771800134>")
            await channel.send("<@&811808231218610266>")
            await channel.send("<@&811802000463101963>")
            break

client.loop.create_task(time_check())

          
client.run('TOKEN') 

【问题讨论】:

  • a) 如果没有随机硬编码的字符串和数字,这将更容易理解,而且每个 if 语句块都是相同的,所以也许它们应该是函数? b)您是否尝试过添加一些打印语句以查看执行的程度?你知道你正在进入while循环吗?你知道你输入了正确的 if 语句吗?
  • 我的意思是,它是 WET 和硬编码的,但这不是问题。问题是他们将函数装饰为一个事件,但他没有调度它。一种解决方案是将事件分派到某处,或者将其作为循环任务运行。

标签: python python-3.x discord discord.py


【解决方案1】:

因为“time_check”不是一个有效的事件。

您可能希望将其作为循环运行。

就这样做吧。

from discord.ext import tasks
 # then on your time_check function, change @client.event for:

@tasks.loop(minutes=1)
async def time_check():
    # the rest of your function

#before client.run()
time_check.start()

希望有效

【讨论】:

  • 好吧。当我运行代码时,它会正常启动,但不会执行任务
猜你喜欢
  • 2021-08-19
  • 2021-01-08
  • 2021-07-10
  • 2019-09-12
  • 2021-11-24
  • 2020-11-23
  • 2021-08-27
  • 2021-09-15
  • 2021-05-09
相关资源
最近更新 更多