當字符串中包含 cutstom 服務器表情符號時,我如何才能讓我的機器人響應?我的代碼目前看起來像這樣:if message.content.startswith(":emoji: Hello"):
await client.get_channel(123456789).send("Hello to you, too!")如果我在 Discord 中發布表情符號和句子,該命令將不會觸發。我需要改變什么?
1 回答

慕碼人2483693
TA貢獻1860條經驗 獲得超9個贊
使用自定義表情符號時,它有一個唯一的 ID,這就是出現在消息內容中的內容,而不僅僅是表情符號的名稱。您可以通過幾種不同的方式訪問它,但最簡單的可能是\:emoji:
,您可以在此處看到它的工作方式:
最后一部分是消息內容將包含的內容。所以在我的例子中,正確的陳述應該是這樣的:
if message.content.lower().startswith("<:statue:709925980029845565> hello"): await message.channel.send("Hello to you too!") # sends to the same channel the message came from
您還可以在不使用其 ID 的情況下檢索表情符號:
emoji = discord.utils.get(message.guild.emojis, name="statue") if message.content.lower().startswith(f"{emoji} hello"): await message.channel.send("Hello to you too!")
請注意,我還使用.lower()
了使命令不區分大小寫的命令,因此無論用戶鍵入HelLo
,HELLO
還是hElLo
添加回答
舉報
0/150
提交
取消