下面是我的不和谐机器人代码。我试图将其更改为服务器中的成员数量的服务器名称。我看着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)
答案 0 :(得分:0)
您需要添加event decorator:
@bot.event
async def on_member_join(member):
...
答案 1 :(得分:0)
您要同时创建Client
和Bot
,则只能使用其中一个。
将您所有的@client
装饰者更改为@bot
装饰者,或者将客户行更改为client = Bot(...)
,并摆脱client = discord.Client()
答案 2 :(得分:0)
我更新到1.5版并对其进行了修复!