首页 > 解决方案 > 为什么我不能在 python 的相同代码中使用命令和事件来制作 Discord bot

问题描述

import discord
from discord.ext import commands
import random

client=commands.Bot(command_prefix=".")

@client.event
async def on_ready():     #tells  me if bot is ready
    print("Bot je spreman")

@client.event
async def on_member_join(member):  #Bot says hi to member in her dm
    await member.create_dm()
    await member.dm_channel.send(f"Bok{member.name}, dobrodošao")

@client.event
async def on_message(message):    # Ignore messages made by the bot
    if message.author == client.user:
        return

    odgovori = ["Dobro sam","Lose sam","OK sam"]

    if message.content == "Kako si":   #Bot responds to message(When i put this block in my code my commands wont work. How do i fix this?)
        odg = random.choice(odgovori)
        await message.channel.send(odg)
        await client.process_commands(message)

@client.command(aliases=["kick"])     #Bot kick people from server
@commands.has_permissions(kick_members = True)
async def k(ctx,member : discord.Member,*,reason= "Bez razloga"):
    await member.send("Izbacen si sa servera, zbog"+reason)
    await member.kick(reason=reason)

@client.command(aliases=["clear"])     #Bot deletes messages
@commands.has_permissions(manage_messages = True)
async def c(ctx,kolicina=2):
    await ctx.channel.purge(limit= kolicina)

@client.command()
async def ping(ctx):            #Bot shows ping
    await ctx.send(f"{round(client.latency * 1000)}ms")



client.run(TOKEN)

代码中的某些内容在克罗地亚语中,例如“odgovori”和一些字符串。我是编码新手,如果有什么东西让你感到抱歉,我很抱歉...................................... ..................................................... .....................................

标签: python

解决方案


推荐阅读