Discord Bot命令不起作用(Python)

时间:2020-08-07 10:48:13

标签: python discord

我目前正在使用python编写我的第一个Discord机器人。尝试编写命令时,我发现自己无法执行此操作。这段代码:

import os
import random
from discord.ext import commands
from dotenv import load_dotenv

load_dotenv()
TOKEN = 'NzQxMjA2OTQzMDc4ODA5NjEy.Xy0Mwg.Shkr9hHvkn-y4l5ye1yTHYM3rQo'
client = discord.Client()
    
@client.event
async def on_member_join(member):
    await member.create_dm()
    await member.dm_channel.send(f'Hi {member.name}, welcome to my Discord server!')

pp = commands.Bot(command_prefix='!')

messages = ["hello", "hi", "how are you?"]
@pp.command(name = "swear")
async def swear(ctx):
    await ctx.send(rand.choice(messages))

client.run(TOKEN)
当我输入!swear时,

在discord应用程序中不输出任何内容。该漫游器处于在线状态,并且可以正常运行其他代码,例如:

@client.event
async def on_member_join(member):
    await member.create_dm()
    await member.dm_channel.send(f'Hi {member.name}, welcome to my Discord server!')

这是完整的代码:

import discord
import os
import random
from discord.ext import commands
from dotenv import load_dotenv

load_dotenv()
TOKEN = (##my token spelled out)
client = discord.Client()

@client.event
async def on_ready():
    print('im in as {0.user}'.format(client))


@client.event
async def on_message(message):
    if message.author == client.user:
        return
 
    if message.content.startswith('hello'):
        await message.channel.send('Hi there!')
        
    elif message.content.startswith("Is Ethan's mum gay?"):
        await message.channel.send("Yep, 100%!")
    
    elif message.content.startswith("What are you doing?"):
        await message.channel.send(f"Step {message.author}")
        
    elif 'happy birthday' in message.content.lower():
        await message.channel.send('Happy Birthday! ??')
        
    
    
@client.event
async def on_member_join(member):
    await member.create_dm()
    await member.dm_channel.send(f'Hi {member.name}, welcome to my Discord server!')

pp = commands.Bot(command_prefix='!')

messages = ["hello", "hi", "how are you?"]
@pp.command(name = "swear")
async def swear(ctx):
    await ctx.send(rand.choice(messages))

client.run(TOKEN)

有人知道我可以解决我的问题吗?

1 个答案:

答案 0 :(得分:1)

Client()Bot()是创建bot的两种方法,同时使用它们是错误的。

使用Client()Bot()创建了两个机器人,但它们需要client.run(TOKEN)pp.run(TOKEN)才能一起运行-但是同时启动它们和可能会引起冲突,哪一个应该获得用户消息。

Bot()Client()的扩展版本,因此您只需要Bot()并使用

  • @pp.event而非@client.event
  • pp.run(TOKEN)而非client.run(TOKEN)
  • pp.user而非client.user

编辑:

并且您必须在所有功能之前定义pp。像这样

import random
from discord.ext import commands

TOKEN = 'TOKEN'

pp = commands.Bot(command_prefix='!')

@pp.event
async def on_ready():
    print('im in as {}'.format(pp.user))

@pp.event
async def on_message(message):
    if message.author == pp.user:
        return
 
    if message.content.startswith('hello'):
        await message.channel.send('Hi there!')
        
    elif message.content.startswith("Is Ethan's mum gay?"):
        await message.channel.send("Yep, 100%!")
    
    elif message.content.startswith("What are you doing?"):
        await message.channel.send(f"Step {message.author}")
        
    elif 'happy birthday' in message.content.lower():
        await message.channel.send('Happy Birthday! ??')
    
@pp.event
async def on_member_join(member):
    await member.create_dm()
    await member.dm_channel.send(f'Hi {member.name}, welcome to my Discord server!')

messages = ["hello", "hi", "how are you?"]

@pp.command(name = "swear")
async def swear(ctx):
    await ctx.send(rand.choice(messages))

pp.run(TOKEN)