Py学习  »  Python

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

BDF_TacticsYT • 2 年前 • 717 次点击  

我正在用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
 
717 次点击  
文章 [ 2 ]  |  最新文章 2 年前
bguest
Reply   •   1 楼
bguest    2 年前

你不需要使用 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    2 年前

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