【问题标题】:discord.py on_member_join not working @bot.eventdiscord.py on_member_join 不工作@bot.event
【发布时间】:2021-01-16 11:35:11
【问题描述】:

以下是我的不和谐机器人代码。我试图让它将服务器名称更改为服务器中的成员数量。我看了this example,但没有用。任何人都可以帮忙吗?我没有收到任何错误,我的输出也在下面。当我尝试为 on_member_join 添加@bot.event 时,它似乎甚至没有到达那里。

输出

Logged in as
Marwin
543266782601936898
------
https://discord.com/oauth2/authorize?client_id=543266782601936898&scope=bot

代码

import discord
import json
import asyncio
from discord.ext.commands import Bot
import aiohttp
from discord.utils import get
import json
from settings import bots
import pyjokes


TOKEN = open("token.txt").readlines()[0].strip()
prefix = "~"
bot = Bot(command_prefix=prefix, description="Ready to serve!")

# Runs when the bot is activated
@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')
    print(discord.utils.oauth_url(bot.user.id))

# @bot.event
# async def on_member_join(member):
#   print("asdsddad")
#   await member.send("Hello!")


@bot.command(pass_context=True)
async def ping(ctx):
    ''' @pong! '''
    await ctx.send('{0} Pong!'.format(ctx.author.mention))

@bot.command(pass_context=True)
async def joke(ctx):
    ''' Tells a random programmer joke '''
    await ctx.send('{0} {1}'.format(ctx.author.mention, pyjokes.get_joke()))

@bot.command(pass_context=True)
async def get_server_id(ctx):
    ''' Get's the server's ID! '''
    await ctx.send('{0}, {1}'.format(ctx.author.mention, ctx.message.guild.id))

@bot.event
async def on_member_join(member):
    print("I made it to join!")
    await member.send('Welcome to the server!')

@bot.command(pass_context=True)
async def members(ctx):
    ''' Get members '''
    members = 0
    for m in ctx.guild.members:
        members = m.guild.member_count
    members -= bots[ctx.message.guild.id]
    await ctx.send('{0} There are '.format(ctx.author.mention)+str(members)+" members")

@bot.command(pass_context=True)
async def change_name(ctx):
    ''' Change the server name! '''
    members = 0
    for m in ctx.guild.members:
        members = m.guild.member_count
    members -= bots[ctx.message.guild.id]
    await ctx.guild.edit(name = "The " + str(members) + " Dwarves")


bot.run(TOKEN)

【问题讨论】:

  • 能否添加任何错误代码、日志文件,或者代码达到什么效果?更多信息将帮助人们回答您的问题。
  • @bob0the0mighty 我已经这样做了

标签: python discord discord.py


【解决方案1】:

您需要添加event decorator:

@bot.event
async def on_member_join(member):
    ...

【讨论】:

  • 我过去曾尝试过,但没有成功。我不知道为什么。
【解决方案2】:

您正在创建ClientBot,您只能使用其中一个。

要么将所有 @client 装饰器更改为 @bot 装饰器,要么将您的客户行更改为 client = Bot(...) 并删除 client = discord.Client()

【讨论】:

  • 我删除了所有这些,但这并没有解决它:(
  • 您的on_member_join 已被注释掉,当我取消注释时,您的代码对我有效
  • 您好,您使用的是 discord.py 1.5 吗?如果是这样,请在网关意图上查看我的回答 here
  • 看来我使用的是 1.1 版
  • v1.1 已于 2 年前发布,请至少更新到 v1.4.2
【解决方案3】:

我更新到 v 1.5 并修复了它!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-16
    • 2021-02-25
    • 1970-01-01
    • 2020-09-19
    • 2021-02-25
    • 2021-02-08
    • 2018-06-10
    • 1970-01-01
    相关资源
    最近更新 更多