discord bot加入消息不起作用

dzjeubhm  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(465)

你好,我尝试了很多命令来修复它,但是机器人什么也没做。这是我的代码。该文件是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'

# )

为什么不起作用?我尝试了很多命令/模型。我用记事本++来编程。给它一个比用python编程更好的程序?也许对初学者来说是什么?

ghhaqwfi

ghhaqwfi1#

你不能两者都用 discord.Clientcommands.Bot ,选择其中一个。 commands.Bot 基本上与 discord.Client 但是有了额外的命令功能
->文件


# 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')

相关问题