社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

如何为python discord机器人导入/使用ctx?

BDF_TacticsYT • 3 年前 • 1370 次点击  

我正在用replit制作一个python discord机器人,但我无法让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']
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/131621
 
1370 次点击  
文章 [ 2 ]  |  最新文章 3 年前
bguest
Reply   •   1 楼
bguest    3 年前

你不需要使用 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')
Max
Reply   •   2 楼
Max    3 年前

如果要使用ctx参数,应该使用discord的命令扩展。皮耶。在 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')