如何导入/使用ctx作为python discordbot



我在replit中制作了一个python discordbot,我无法让ctx工作我尝试了import ctx,但只得到了一条错误消息,如AttributeError:模块"ctx"没有属性"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']

如果你想使用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')

您不需要使用ctx库来访问message变量的guild属性。尝试以下操作:

@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')

最新更新