【发布时间】: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