我有一个Discord机器人,它在消息通道中存储用户的截止日期。截止日期结束后,我希望该机器人通知主持人,以便他们进行处理。
除此以外,机器人的所有操作均已完成。我对这个问题的想法是只具有一个功能,该功能每24小时检查一次带有截止日期的频道,并在该频道上找到与当前日期相同的日期,并取出用户ID(也存储在所述消息中)。 我愿意为这个问题提供其他解决方案。
我已阅读并在Google上搜索了一堆。至少在我看来,time.sleep()
和schedule.every
会在这段时间内停止我的整个程序,但是也许我对计划的事情不对,因为我试图将其实现到我的代码中,但我不知道如何使其工作。我只有一个星期的Python经验,请耐心等待。
如果有帮助的话,这里几乎是我的整个代码(当然没有bot令牌部分)
import datetime
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import random
from discord import Game
from discord.utils import get
from datetime import datetime, timedelta
Client = discord.client
client = commands.Bot(command_prefix = '%')
Clientdiscord = discord.Client()
@client.event
async def on_ready():
await client.change_presence(game=Game(name='with nuclear waste'))
print('Ready, bitch')
@client.event
async def on_reaction_add(reaction, user):
if ' has sent an application to become a full fledged FRICK' in reaction.message.content:
Name = reaction.message.content.split('#')
usid = reaction.message.content.split('=')
usid1 = usid[1].split(' ')
user =reaction.message.server.get_member(usid1[0])
msg = reaction.message.content.split('?')
eventcode = str (msg[1])
cont = str(msg[0])
kakapoopoo = '@' + usid1[0]
#await client.send_message(user, 'test')
if reaction.emoji == get(client.get_all_emojis(), name='FeelsBadMan'):
await client.send_message(client.get_channel('560678137630031872'), Name[0] + ' has attented 1 event.')
await client.edit_message(reaction.message, cont + '?01')
if reaction.emoji == get(client.get_all_emojis(), name='veetsmug'):
await client.send_message(client.get_channel('560678137630031872'), Name[0] + ' has attented 2 events.')
await client.edit_message(reaction.message, cont + '?10')
if reaction.emoji == get(client.get_all_emojis(), name='POGGERS'):
await client.edit_message(reaction.message, cont + '?11')
await client.send_message(client.get_channel('560678137630031872'), Name[0] + ' has attented 3 events.')
if reaction.emoji == get(client.get_all_emojis(), name='HYPERS'):
role1 = discord.utils.get(reaction.message.server.roles, name='Pending Frick')
await client.remove_roles(user, role1)
await client.send_message(client.get_channel('560678137630031872'), user.mention + ' has attented 4 events and is now a ***FRICK***.\n@here')
role2 = discord.utils.get(reaction.message.server.roles, id='561087733834186754')
await client.add_roles(user, role2)
await client.send_message(user, 'You are now a full fledged ***FRICK***')
await client.delete_message(reaction.message)
elif 'To approve screenshot react with :HYPERS: to dissaprove react with :FeelsBadMan:' in reaction.message.content:
cunt = reaction.message.content.split('#')
name = cunt[0]
idududu = reaction.message.content.split('?')
peepee = idududu[1]
#await client.send_message(client.get_channel('560678137630031872'), 'test' + peepee)
idud = peepee.split('\nThe events the user has attented before are:')
usid = idud[0]
#await client.send_message(client.get_channel('560678137630031872'), 'test' + usid)
#await client.send_message(client.get_channel('560678137630031872'), 'test' +str(usid))
user=await client.get_user_info(usid)
event = reaction.message.content.split('has attented a ')
eventu = event[1]
evento = eventu.split('Screenshot:')
if reaction.emoji == get(client.get_all_emojis(), name='HYPERS'):
await client.send_message(user, 'Your screenshot has been approved')
await client.delete_message(reaction.message)
async for message in client.logs_from(discord.Object(id='561667365927124992'), limit = 100):
#await client.send_message(user, 'test')
if name in message.content:
if ' eventcode?00' in message.content:
await client.send_message(user, 'You have attented 1 event')
emoji = get(client.get_all_emojis(), name='FeelsBadMan')
await client.add_reaction(message, emoji)
await client.send_message(discord.Object(id='562607755358371840'), str(usid) + ' ' + str(evento[0]))
elif ' eventcode?01' in message.content:
await client.send_message(user, 'You have attented 2 events')
emoji = get(client.get_all_emojis(), name='veetsmug')
await client.add_reaction(message, emoji)
async for message in client.logs_from(discord.Object(id='562607755358371840'), limit = 100):
if usid in message.content:
cont = message.content
await client.edit_message(message, cont + ', ' + str(evento[0]))
elif ' eventcode?10' in message.content:
await client.send_message(user, 'You have attented 3 events')
emoji = get(client.get_all_emojis(), name='POGGERS')
await client.add_reaction(message, emoji)
async for message in client.logs_from(discord.Object(id='562607755358371840'), limit = 100):
if usid in message.content:
cont = message.content
await client.edit_message(message, cont + ', ' + str(evento[0]))
elif ' eventcode?11' in message.content:
emoji = get(client.get_all_emojis(), name='HYPERS')
await client.add_reaction(message, emoji)
async for message in client.logs_from(discord.Object(id='562607755358371840'), limit = 100):
if usid in message.content:
await client.delete_message(message)
elif reaction.emoji == get(client.get_all_emojis(), name='FeelsBadMan'):
await client.send_message(user, 'Your screenshot has not been approved')
await client.delete_message(reaction.message)
@client.event
async def on_message(message):
if message.content == '%start':
if "561047714230435871" in [role.id for role in message.author.roles]:
deadline = datetime.now() + timedelta(days=21)
author = message.author
mes = str(author) + ' has sent an application to become a full fledged FRICK id ='+ str(message.author.id) + ' Deadline+' +str(deadline.strftime("%Y-%m-%d")) + ' eventcode?00'
emoji = get(client.get_all_emojis(), name='HYPERS')
await client.add_reaction(message, emoji)
role = get(message.server.roles, id='561055432748302336')
await client.add_roles(message.author, role)
await client.send_message(client.get_channel('561667365927124992'), mes)
await client.send_message(message.author, '***You have entered the Frickling program!**\n\nTo become a full fledged Frick you must attend 4 guild/alliance events. Please provide screensots to the bot proving that you have attented them by using %attended command followed by a screenshot **LINK** and the name of the activity.\n\n Example: %attented https://cdn.discordapp.com/attachments/530909412320083969/558085258164305921/guild_event_2.jpg fame farm')
role = get(message.server.roles, name='Frickling')
await client.remove_roles(message.author, role)
elif "561055432748302336" in [role.id for role in message.author.roles]:
await client.send_message(message.channel,'Seems like you already started the application process for becoming a full fledged Frick')
else:
await client.send_message(message.channel,'Seems like you do not have the permisssions to use me.\n\nIt might be because your application for Frickling has not yet been approved or you are already a full fledged Frick')
elif message.content == 'Tell that bot to get lost':
if message.author == message.server.get_member('336858563064496130'):
await client.send_message(message.channel,'Get lost, stupid bot')
elif message.content == 'Tell Meatcup to get lost':
if message.author == message.server.get_member('336858563064496130'):
user = discord.utils.get(reaction.message.server.members, id ='331864154803666945')
await client.send_message(user, 'Get lost, Meatcup')
elif message.content == '%cunt':
await client.send_message(message.channel,'Yes, my master?')
emoji = get(client.get_all_emojis(), name='HYPERS')
await client.add_reaction(message, emoji)
elif message.content == '%testfrick':
if "561087733834186754" in [role.id for role in message.author.roles]:
await client.send_message(message.channel,'You are a frick')
else:
await client.send_message(message.channel,'Error')
elif message.content == '%testpend':
if "561055432748302336" in [role.id for role in message.author.roles]:
await client.send_message(message.channel,'You are pending')
else:
await client.send_message(message.channel,'Error')
elif '%attended' in message.content and message.author.id != '560634369094582302':
author = message.author
authid = message.author.id
cunt = 0
msg = message.content.split(' ',2)
if len(msg) > 2:
emoji = get(client.get_all_emojis(), name='HYPERS')
await client.add_reaction(message, emoji)
for i in client.servers:
for x in i.roles:
if x.id == '561055432748302336':
async for message in client.logs_from(discord.Object(id='562607755358371840'), limit = 100):
if authid in message.content:
eventu = message.content.split(' ',1)
cunt = 1
await client.send_message(discord.Object(id='560679934209687552'), str(author) + ' has attented a ' + msg[2] + '\nScreenshot:' + msg[1] + '\nTo approve screenshot react with :HYPERS: to dissaprove react with :FeelsBadMan:\nUserid?'+authid + '\nThe events the user has attented before are: ' + str(eventu[1]))
if cunt == 0:
await client.send_message(discord.Object(id='560679934209687552'), str(author) + ' has attented a ' + msg[2] + '\nScreenshot:' + msg[1] + '\nTo approve screenshot react with :HYPERS: to dissaprove react with :FeelsBadMan:\nUserid?'+ authid)
else:
await client.send_message(message.author,'Error. You did not provide the name of the event or you only provided the name of the event.')
答案 0 :(得分:1)
线程使您可以“同时”处理2件事(或更多)。当一个线程进入睡眠状态时,另一线程可以执行“其余”操作
答案 1 :(得分:1)
对于异步python,当需要非阻塞睡眠时,可以使用方法asyncio.sleep(n)
,n是希望该方法睡眠的秒数。因此,每24小时睡眠一次的基本异步方法如下所示:
async def daily_task():
print('This method will NOT block')
await asyncio.sleep(24*60*60)
编辑:感谢Benjin,他指出我忘了await
的睡眠。
答案 2 :(得分:0)
您可以使用cron api来执行此操作,而无需在python中进行监视 https://crontab.guru/#0_ / 24 _ _ _