【发布时间】:2021-11-23 04:23:03
【问题描述】:
我刚开始学习 python,我的 discord bot 不会上线。它只是说 “进程退出,退出代码为 0”。并且代码没有错误。
这是我的代码。 enter image description here
【问题讨论】:
-
请将代码添加为
code block,而不是图片。
标签: python discord discord.py
我刚开始学习 python,我的 discord bot 不会上线。它只是说 “进程退出,退出代码为 0”。并且代码没有错误。
这是我的代码。 enter image description here
【问题讨论】:
code block,而不是图片。
标签: python discord discord.py
添加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")
【讨论】:
你应该试试这个
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)
【讨论】:
你给的代码是错误的,这是正确的:
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 的文档。
【讨论】: