我希望能够在特定的不一致频道中找到最早的消息,而不是绑定到不一致命令.

我试过使用discord机器人,但似乎它们主要是用来执行机器人命令的,所以我怎么能随时这样做呢?

我试过以下几种方法:

import discord

client = Client(intents=discord.Intents.all())

async def getfirstmessage():
    channel = client.get_guild(guildid).get_channel(channelid)
    messages = [message async for message in channel.history(limit=1, oldest_first=True)]
    print(messages)

我收到了这样的错误:

RuntimeWarning: coroutine 'getfirstmessage' was never awaited getfirstmessage()

当我try 在‘getfirst stMessage’函数前面添加等待时,如果收到以下错误:

SyntaxError: 'await' outside function

先谢谢你.

推荐答案

import discord
import asyncio

intents = discord.Intents.default()
intents.message_content = True  # Enable message content for intents

client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print(f'Logged in as {client.user.name}')
    await get_first_message()

async def get_first_message():
    guild_id = 1234567890  # Replace with your guild ID
    channel_id = 1234567890  # Replace with your channel ID

    guild = client.get_guild(guild_id)
    channel = guild.get_channel(channel_id)

    async for message in channel.history(limit=1, oldest_first=True):
        print(f'Earliest message: {message.content}')
        break  # Exit the loop after retrieving the first message

# Run the bot
loop = asyncio.get_event_loop()
loop.run_until_complete(client.start('YOUR_BOT_TOKEN'))  # Replace with your bot token
# Make sure to replace guild_id, channel_id, and 'YOUR_BOT_TOKEN' with your specific values.

Python相关问答推荐

如何使用上下文管理器创建类的实例?

比较两个二元组列表,NP.isin

Deliveryter Notebook -无法在for循环中更新matplotlib情节(保留之前的情节),也无法使用动画子功能对情节进行动画

将整组数组拆分为最小值与最大值之和的子数组

Python上的Instagram API:缺少client_id参数"

运行Python脚本时,用作命令行参数的SON文本

2D空间中的反旋算法

处理带有间隙(空)的duckDB上的重复副本并有效填充它们

基于索引值的Pandas DataFrame条件填充

将9个3x3矩阵按特定顺序排列成9x9矩阵

将输入聚合到统一词典中

Pandas—在数据透视表中占总数的百分比

pandas在第1列的id,第2列的标题,第3列的值,第3列的值?

在输入行运行时停止代码

(Python/Pandas)基于列中非缺失值的子集DataFrame

并行编程:同步进程

Django Table—如果项目是唯一的,则单行

获取PANDA GROUP BY转换中的组的名称

为什么dict. items()可以快速查找?

当我定义一个继承的类时,我可以避免使用`metaclass=`吗?