简体   繁体   English

Python discord bot 在打开后立即关闭/崩溃

[英]Python discord bot closes/crashes immediately after opening

I'm trying to make a discord bot with discord.py for my discord server, I have extremely basic code that should get the bot online but instead just opens and then crashes.我正在尝试为我的不和谐服务器制作一个带有discord.py的不和谐机器人,我有非常基本的代码可以让机器人在线,但只是打开然后崩溃。

import discord

@client.event
asnyc def on_ready():
    print 'bot read`enter code here`y'

client.run('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')

I do have the token normally I just didn't want to leak.我确实有令牌,我只是不想泄漏。 I appreciate all the help.我感谢所有的帮助。

Your code should look like this:您的代码应如下所示:

import discord
from discord.ext import commands

@client.event
async def on_ready():
    print("ready")


bot.run('TOKEN')

I found a nice tutorial to create a discord bot in python.我找到了一个很好的教程来在 python 中创建一个不和谐的机器人。 Therefore you can create a connection with the following code:因此,您可以使用以下代码创建连接:

import os

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')

client = discord.Client()

@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')

client.run(TOKEN)

And I would suggest to rename the file from 'discord' to 'bot' or something else, so there are no conflicts with the import.我建议将文件从“discord”重命名为“bot”或其他名称,这样就不会与导入发生冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM