[英]Im trying to make a discord bot that sends a message reminding people that our secret santa is in 1 week but its isnt working
I've tried what i can remember but i'm still fairly new to discord.py so I cant spot what the problem is.我已经尝试了我能记住的东西,但我对 discord.py 还是很陌生,所以我无法发现问题所在。
import os
import asyncio, datetime
from keep_alive import keep_alive
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as {0}!'.format(self.user))
asyncio.datetime.datetime((2021, 12, 10, 20, 41)
channel = (894709862091591724)
channel.message.send('@everyone the secret santa is in 1 week!'))
client = MyClient()
keep_alive()
client.run(os.getenv('TOKEN'))
If you want to send messages automatically ( for example every day ).如果您想自动发送消息(例如每天)。 You can use tasks.loop
for it.您可以使用tasks.loop
。 It will run every 24 hours and send your message.它将每 24 小时运行一次并发送您的消息。
Also, if you make a simple bot and you don't have to use classes it's much easier to create a client with client = commands.Bot()
instead of making a class.此外,如果您制作了一个简单的机器人并且您不必使用类,那么使用client = commands.Bot()
创建客户端会比制作 class 更容易。 You also have to use client.get_channel(id)
or await client.fetch_channel(id)
to use channel.您还必须使用client.get_channel(id)
或await client.fetch_channel(id)
才能使用频道。
# imports you need
import discord, asyncio, datetime
from discord.ext import commands, tasks
from keep_alive import keep_alive
client = commands.Bot() # creating client
@tasks.loop(hours=24) # you can change it to minutes or seconds too
async def secret_santa():
santa = datetime.date(2021, 12, 6) # your date (year, month, day)
today = datetime.date.today() # gets today's date
delta = (santa - today).days # calculates number of days to "santa" (I used ".days" to get only number of days)
channel = await client.fetch_channel(894709862091591724)
await channel.send(f'@everyone the secret santa is in {delta} days')
secret_santa.start() # starting task
keep_alive()
client.run(os.getenv('TOKEN'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.