简体   繁体   English

我正在尝试制作一个 discord 机器人,它会发送一条消息,提醒人们我们的秘密圣诞老人在 1 周内,但它不起作用

[英]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.

相关问题 我正在尝试制作蛇,但这不起作用 - Im trying to make snake and this isnt working 试图制作一个不和谐的机器人,但 on.message 或 message.content 不工作 - Trying to make a discord bot but on.message or message.content not working 试图制作一个在随机时间发送随机消息的 discord 机器人,但我不断收到此错误 - Trying to make a discord bot that sends a random message at a random time, but I keep getting this error 如何让我的自定义不和谐机器人发送消息 - How to make my custom discord bot sends message 试图制作一个特殊的 Discord 机器人消息 - Trying to make a special Discord bot message Python中的Discord Bot-我试图从具有特定角色的所有人中随机挑选一个人 - Discord Bot in Python - Im trying to pick a random person from all the people with a specific role 我正在尝试制作一个简单的 discord 机器人来加入和离开语音频道,但我不能让机器人离开频道 - Im trying to make a simple discord bot that joins and leaves a voice channel, but I cant make the bot leave the channel Discord py,机器人发送了两次消息 - Discord py, the bot sends a message twice 我需要让一个不和谐的机器人停止它的进程并等待几秒钟,如果人们发送它的命令 - I need to make a discord bot stop its process and wait a few seconds if people spam its command Discord.py - 让机器人对自己的消息做出反应 - Discord.py - Make a bot react to its own message(s)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM