@ client.command下面的功能不起作用,但@ client.event下面的功能起作用

时间:2019-08-23 13:14:14

标签: python-3.x discord.py

我没有从 @ client.command 下面编写的函数获得任何响应。但是在 @ client.event 下面,功能仍在起作用。例如ping命令,当我键入 .ping 时它没有反应,但是如果键入 .hello ,它是 @ client.event 下的一个函数,就可以了

调用join函数时,我试图登录到控制台,但是它甚至没有将日志打印到控制台。

@client.command(pass_context=True)
async def join(ctx):
    channel = ctx.author.message.voice.channel
    await channel.connect()
    print("Joined to {channel})

还有ping命令;

@client.command(pass_context=True)
async def ping(ctx):
     await ctx.send(f"{round(client.latency * 1000)} ms")

仅工作块;

@client.event
async def on_message(message):
     if message.content.startswith('.hello'):
         channel = message.channel
         print(f"[*] {message.author.name} ({message.author}): {message.content}")
         await channel.send('Hello there!')

完整的代码在这里:

import discord
from discord.utils import get
from discord.ext import commands
from discord.ext.commands import Bot
from discord.voice_client import VoiceClient
import asyncio

TOKEN = 'token here'
client = commands.Bot(command_prefix = '.')

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

@client.command(pass_context=True)
async def join(ctx):
    channel = ctx.author.message.voice.channel
    await channel.connect()
    print(f"Joined to {channel}")

@client.command(pass_context=True)
async def ping(ctx):
    await ctx.send(f"{round(client.latency * 1000)} ms")

@client.event
async def on_message(message):
    if message.content.startswith('.hello'):
        channel = message.channel
        print(f"[*] {message.author.name} ({message.author}): {message.content}")
        await channel.send('Hello there!')

client.run(TOKEN)

0 个答案:

没有答案