5 回答

TA貢獻1809條經驗 獲得超8個贊
你需要舉辦你的活動。sendmessage.starton_ready
例子:
@client.event
async def on_ready():
sendmessage.start()

TA貢獻1744條經驗 獲得超4個贊
import discord
from discord.ext import commands, tasks
bot = commands.Bot('!') # ! = PREFIX
@tasks.loop(seconds=10)
async def sendmessage():
channel = bot.get_channel(123456789) #Channel ID
await channel.send("hello")
sendmessage.start()
bot.run("TOKEN")

TA貢獻1806條經驗 獲得超5個贊
您可能想嘗試將通道移動到函數中,如下所示:
import discord
import datetime
from discord.ext import commands, tasks
TOKEN = 'token'
client = discord.Client()
@tasks.loop(seconds=10)
async def sendmessage():
channel = client.get_channel('channel id')
await channel.send("hello")
sendmessage.start()
client.run(TOKEN)
該函數僅接受整數作為輸入。get_channel

TA貢獻1993條經驗 獲得超6個贊
確保你的“頻道 ID”是一個整數,并且假設你有某個地方,你的代碼應該按原樣工作。sendmessage.start()
額外注意:您可能想將自己的任務放入任務中。從內部緩存獲取通道對象,因此如果緩存過期,您的通道對象也會過期。get_channel
get_channel

TA貢獻1817條經驗 獲得超6個贊
您可以使用 '.start() 啟動任務
channel = client.get_channel('channel id')
@tasks.loop(seconds=10)
async def sendmessage():
await channel.send("hello")
sendmessage.start()
添加回答
舉報