【问题标题】:Discord bot won't go onlineDiscord 机器人不会上线
【发布时间】:2021-11-23 04:23:03
【问题描述】:

我刚开始学习 python,我的 discord bot 不会上线。它只是说 “进程退出,退出代码为 0”。并且代码没有错误。

这是我的代码。 enter image description here

【问题讨论】:

  • 请将代码添加为code block,而不是图片。

标签: python discord discord.py


【解决方案1】:

添加await client.process_commands(ctx) 和@client.event 不需要括号()。使用下面给出的代码:

@client.event
async def on_message(ctx):
  if ctx.author == client.user:
    return

  await client.process_commands(ctx)

如果仍然无法正常工作,请使用整个代码:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="!")


@bot.event
async def on_ready():
  print('We are logged in!')

@bot.event
async def on_message(message):
   if message.author==bot.user: 
     return

   await bot.process_commands(message)

@bot.command()
async def ping(message) :
   await message.channel.send("Pong!!")

bot.run("TOKEN")

【讨论】:

  • 谢谢,但机器人仍然离线。
  • 我已将我的帖子编辑为整个代码,试试这个。
  • 还是不行。代码中没有错误,在我运行程序后它只是说“进程以退出代码0完成”。
  • 这意味着代码中没有错误,但您必须在 IDE 的设置中检查:stackoverflow.com/a/61883867/16203851
【解决方案2】:

你应该试试这个

import os
import discord
from discord.ext import commands

discord_token = "Your Token"

client = discord.Client()

bot = commands.Bot(cloient_prefix="!")

@client.command()
async def on_ready():
  print("running")

other codes here...

bot.run(discord_token)

【讨论】:

    【解决方案3】:

    你给的代码是错误的,这是正确的:

    import os 
    import discord
    from discord.ext import commands
    
    bot = commands.Bot(command_prefix="!") # you don't need an extra discord.client Bot is enough
    token = os.getenv("TOKEN")
    #you should not run the program here you should run it at last
    
    @bot.event #no need for brackets
    async def on_ready():
      print("running")
    
    @bot.event
    async def on_message(ctx):
      if ctx.author == client.user:
        return
    
    @bot.command(name="span",description="YOUR DESCRIPTION HERE") # it is command and not commands
    async def span_(ctx,amount:int,*,message):
      for i in range(amount):
        await ctx.send(message)
    
    bot.run(token) #you should run the bot now only and this will work perfectly!
    

    您可以找到 discord.py here 的文档。

    【讨论】:

    • 谢谢,但机器人仍然离线。未发现错误
    • 你应该有一个 .env 文件这个令牌,否则这将不起作用
    猜你喜欢
    • 2021-11-08
    • 2021-04-01
    • 2020-12-27
    • 2018-07-22
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    相关资源
    最近更新 更多