Discord Bot加入消息不起作用python

时间:2021-07-27 12:08:14

标签: python discord.py

你好,我尝试了很多命令来修复它,但机器人什么都不做 这是我的代码。文件是 bot.py

import discord
import os 
from discord.ext import commands

class MyClient(discord.Client):
    async def on_ready(self):
        print('Logged on as {0}!'.format(self.user))

    async def on_message(self, message):
        print('Message from {0.author}: {0.content}'.format(message))

client = MyClient()

@client.event
async def on_ready():
    print(f'{client.user.name} Test!')
    
    
###############

bot = commands.Bot(command_prefix="!")
@bot.event
async def on_guild_join(guild):
    if guild.system_channel:
        await guild.system_channel.send("Test")

#@client.event
#async def on_member_join(member):
#    await ctx.author.send("Welcome!")

##*@client.event
#async def on_member_join(member):
   
#   await member.create_dm()
   
   
#   await member.send(
#   f'Hallo{member.name}, Willkommen'
#   )


为什么它不起作用我尝试了很多命令/模型。 我使用 Notepad++ 进行编程。给它一个更好的程序来用 Python 编程?也许初学者应该怎么做?

1 个答案:

答案 0 :(得分:0)

您不能同时使用 discord.Clientcommands.Bot,请选择其中之一。

commands.Botdiscord.Client 基本相同,但具有额外的命令功能
-> Docs


# so first remove the client part in your code

# and define the Bot | you can name it 'bot' or 'client' 
# bot = commands.Bot(command_prefix="!")
# is the same as 
# client = commands.Bot(command_prefix="!")

# also make sure to enable Intents
intents = discord.Intents.default()
intents.member = True
bot = commands.Bot(command_prefix="!", intents=intents)


#now you can add events
@bot.event
async def on_guild_join(guild):
    if guild.system_channel:
        await guild.system_channel.send("Test")

@bot.event
async def on_member_join(member):
    await member.send("welcome!")


# and commands
@bot.command()
async def slap(ctx, member: discord.Member):
    await ctx.send(f"{ctx.author} gave {member} a slap")



# at the very end run your bot
bot.run('BOT TOKEN')