我在 replit 中制作了一個 python discord bot,我無法讓 ctx 作業,我嘗試過import ctx,但我收到一條錯誤訊息,如AttributeError: module 'ctx' has no attribute 'message' 這是我的代碼:
import discord
import ctx
import os
client = discord.Client()
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith(""):
print(message.content)
guild = ctx.message.guild
await guild.create_text_channel('cool-channel')
client.run(os.getenv('TOKEN'))
my_secret = os.environ['TOKEN']
uj5u.com熱心網友回復:
您不應該需要使用ctx庫來訪問message變數的公會屬性。請嘗試以下操作:
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith(""):
print(message.content)
guild = message.guild
await guild.create_text_channel('cool-channel')
uj5u.com熱心網友回復:
如果要使用 ctx 引數,則應該使用 discord.py 的命令擴展名。在on_message事件中,你可以使用message引數“為CTX引數”。
@client.event
async def on_message(message):
if message.author != client.user:
if message.content.startswith(""):
print(message.content)
guild = message.guild
await guild.create_text_channel('cool-channel')
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/367968.html
