因此,我正在制作一个基本的bot命令,该命令以玩家所说的内容作为响应,就像执行!test代码一样会使bot以“ code”作为响应。由于某种原因,运行命令时什么也不会发生。我什至在其中放了一张打印纸,以查看它是否确实在运行,但实际上不是。这是我的代码:
import discord
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix="!")
@client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
print("-"*16)
game = discord.Game("Discord")
await client.change_presence(status=discord.Status.online, activity=game)
@bot.command()
async def test(ctx, arg):
await ctx.send(str(arg))
client.run('token here')
感谢您的帮助。
答案 0 :(得分:1)
尝试一下:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="!")
@client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
print("-"*16)
game = discord.Game("Discord")
await client.change_presence(status=discord.Status.online, activity=game)
@client.command()
async def test(ctx, *, arg):
await ctx.send(str(arg))
client.run('token here')
这是您弄错的地方:
client = discord.Client()
bot = commands.Bot(command_prefix="!")
您有2个单独的bot处理程序,如果使用命令,则只需要bot = commands.Bot(command_prefix="!")
行,在这种情况下,您有bot的命令处理程序,但是您正在运行客户端
答案 1 :(得分:0)
下面的代码在使用Python3.7进行测试时按预期运行...
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="!")
@client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
print("-"*16)
game = discord.Game("Discord")
await client.change_presence(status=discord.Status.online, activity=game)
@client.command()
async def test(ctx, *arg):
await ctx.send(str(arg))
client.run('token here')
您发布的代码在函数之外具有await语句
......
print("-"*16)
game = discord.Game("Discord")
await client.change_presence(status=discord.Status.online, activity=game)
.......
也要更改
@bot.command()
async def test(ctx, arg):
收件人:
@bot.command()
async def test(ctx, *arg):
有关您为什么通过*arg
而不通过arg
的解释:
答案 2 :(得分:0)
如果要让机器人运行最后一行代码,则不能同时运行客户端和bot;如果要让客户机运行,请使用bot.run('your token')< / p>