简体   繁体   English

discord.py on_member_join 不起作用@bot.event

[英]discord.py on_member_join not working @bot.event

Below is my code for a discord bot.以下是我的不和谐机器人代码。 I am trying to get it to change the server name to the amount of members in the server.我试图让它将服务器名称更改为服务器中的成员数量。 I looked at this example but it did not work.我看了这个例子,但没有用。 Can anyone help?任何人都可以帮忙吗? I do not receive any errors, and my output is also below.我没有收到任何错误,我的输出也在下面。 When I try and add the @bot.event for on_member_join, it does not seem to even get there.当我尝试为 on_member_join 添加 @bot.event 时,它似乎甚至没有到达那里。

Output输出

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

Code代码

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)

You need to add the event decorator :您需要添加事件装饰器

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

You are creating both a Client and a Bot , you should only use one or the other.您正在创建ClientBot ,您应该只使用其中一个。

Either change all of your @client decorators to @bot decorators or change your client line to client = Bot(...) and get rid of client = discord.Client()要么将所有@client装饰器更改为@bot装饰器,要么将您的客户端行更改为client = Bot(...)并摆脱client = discord.Client()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM