【问题标题】:Discord Bot Joining Message is not working pythonDiscord Bot加入消息不起作用python
【发布时间】:2021-07-27 12:08:14
【问题描述】:

您好,我尝试了很多命令来修复它,但机器人什么也没做 这是我的代码。文件是 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 编程?初学者应该怎么做?

【问题讨论】:

  • 试试 PyCharm、Visual Studio Code 或类似的东西。你启用了 Intent 吗?
  • 好的,谢谢,不,但我现在添加```intents = discord.Intents.default() intents.members = True client = commands.Bot(command_prefix=',', intents=intents) ``` 但仍然无法正常工作:/
  • 您是否尝试过使用print 声明?比如:print(f"Joined {guild}.") 看看这个事件是否有效?
  • 这可能会有所帮助:stackoverflow.com/questions/60525993/…
  • @Dominik 它仍然无法正常工作查看第二个答案中的代码:(。我也尝试打印:/

标签: python discord.py


【解决方案1】:

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

【讨论】:

  • 朋友加入机器人时遇到同样的问题,什么也不做。完全没用。我不知道也许我对它编程很愚蠢。在我使用 C# 编程之前,但这对于初学者来说是难以置信的。
  • 如果要使用on_member_join事件需要将intents.members设置为True,或者Friend is Joining the bot是什么意思?如果 bot 没有发送“测试”,可能是公会没有系统频道
  • 请使用本网站规则要求的英语。 How to deal with non-english content
  • @Guddi 这些家伙删除了 cmets,因为我们用德语说话 xDD 没关系。我和会员一起读过。该机器人正在启动,但仍然无法正常工作。我打开了不和谐的开发人员集成,但仍然无法正常工作:/
  • @Guddi 不,机器人正在启动。我在 Discord 中创建了一个测试帐户,但仍然没有来自 Bot 的消息...我再次添加以下程序代码:@client.event async def on_ready(): print(f'{client.user.name} Test!') @client.event async def on_member_join(member): await member.send('welcome !') @client.event async def on_guild_join(guild): owner = guild.owner await owner.send("hi there!") 也许成员是问题?当任何人加入服务器或我需要创建此角色时,成员是角色吗?
猜你喜欢
  • 2021-06-16
  • 2021-03-27
  • 2021-08-12
  • 2020-04-18
  • 2021-10-30
  • 2020-08-25
  • 2018-07-07
  • 2021-10-22
  • 1970-01-01
相关资源
最近更新 更多