尝试接收在不同命令中创建的特定频道内的所有消息
Trying to receive all messages within a specific channel created in a different command
我目前正在编写一个机器人,它允许两个人在全球范围内、在一个服务器内、在一个帐户上或与一个机器人对弈。目前,我执行此操作的方法涉及配对算法(这不是最复杂的),然后它会创建一个频道供玩家发送消息,或者在全球游戏的情况下在不同服务器上创建两个带有 webhook 的频道。我的问题是:每当通过这些渠道之一发送消息时,我将如何处理 运行 代码?具体来说,我需要通过全局游戏中的网络钩子将一个通道中发送的每条消息的副本发送到另一个通道,以便两个玩家都可以进行交流并检查输入消息是否采用移动格式,在这种情况下机器人会采取行动并将消息复制到另一个频道。我考虑过使用
@bot.event
async def on_message(mes)
但我不能在全球范围内这样做,因为在全球范围内不会知道检查消息的渠道,而且我不知道它是否可以在函数中以一种可以检查的方式定义频道要么。谢谢!
@bot.event
async def on_message(message):
if message.channel.id in my_list_of_channel_ids:
# It's a chess channel
# Now you can check if the message contains a valid move
或者您可以使用 bot.wait_for
# The `is_game_over` var indicates if the game is over
while not is_game_over:
def check(message):
"""Checks if the message contains a valid move,
you should also check if `message.author == `ctx.author`
if you're doing this in a command
and if message.channel is the same as the channel you created before"""
return message.content.upper() in ['A1', 'A2', 'A3'] # Put all the valid moves here
message = await bot.wait_for('message', check=check)
# from here do your thing, add the moves, check if someone won the game, etc...
如果没有您当前拥有的代码片段,就不能更具体
我目前正在编写一个机器人,它允许两个人在全球范围内、在一个服务器内、在一个帐户上或与一个机器人对弈。目前,我执行此操作的方法涉及配对算法(这不是最复杂的),然后它会创建一个频道供玩家发送消息,或者在全球游戏的情况下在不同服务器上创建两个带有 webhook 的频道。我的问题是:每当通过这些渠道之一发送消息时,我将如何处理 运行 代码?具体来说,我需要通过全局游戏中的网络钩子将一个通道中发送的每条消息的副本发送到另一个通道,以便两个玩家都可以进行交流并检查输入消息是否采用移动格式,在这种情况下机器人会采取行动并将消息复制到另一个频道。我考虑过使用
@bot.event
async def on_message(mes)
但我不能在全球范围内这样做,因为在全球范围内不会知道检查消息的渠道,而且我不知道它是否可以在函数中以一种可以检查的方式定义频道要么。谢谢!
@bot.event
async def on_message(message):
if message.channel.id in my_list_of_channel_ids:
# It's a chess channel
# Now you can check if the message contains a valid move
或者您可以使用 bot.wait_for
# The `is_game_over` var indicates if the game is over
while not is_game_over:
def check(message):
"""Checks if the message contains a valid move,
you should also check if `message.author == `ctx.author`
if you're doing this in a command
and if message.channel is the same as the channel you created before"""
return message.content.upper() in ['A1', 'A2', 'A3'] # Put all the valid moves here
message = await bot.wait_for('message', check=check)
# from here do your thing, add the moves, check if someone won the game, etc...
如果没有您当前拥有的代码片段,就不能更具体