【发布时间】:2021-05-06 17:25:25
【问题描述】:
我不确定代码是否因为on_message(ctx) 而无法工作,不管怎样,这就是我所做的:
import discord
from discord.ext import commands
import pymongo
import os
from pymongo import MongoClient
mango_url = "mongodb+srv://<usernamehere>:<password>@discordbot.kllv6.mongodb.net/discordbot?
retryWrites=true&w=majority"
cluster = MongoClient(mango_url)
db = cluster["discordbot"]
collection = db["discordbot"]
client = commands.Bot(command_prefix = '$')
@client.event
async def on_ready():
print("bot is online")
@client.command()
async def _ping(ctx):
await ctx.send('discord sucks :(')
@client.event
async def on_message(ctx):
author_id = ctx.author.id
guild_id = ctx.guild.id
author = ctx.author
user_id = {"_id": author_id}
if ctx.author == client.user:
return
if ctx.author.bot:
return
if(collection.count_documents({}) == 0):
user_info = {"_id": author_id, "GuildID": guild_id, "Level": 1, "XP": 0}
collection.insert_one(user_info)
if(collection.count_documents(user_id) == 0):
user_info = {"_id": author_id, "GuildID": guild_id, "Level": 1, "XP": 0}
collection.insert_one(user_info)
exp = collection.find(user_id)
for xp in exp:
cur_xp = xp["XP"]
new_xp = cur_xp + 1
collection.update_one({"_id": author_id}, {"$set":{"XP":new_xp}}, upsert=True)
#await ctx.channel.send("1 xp up")
lvl = collection.find(user_id)
for levl in lvl:
lvl_start = levl["Level"]
new_level = lvl_start + 1
if cur_xp >= round(5 * (lvl_start ** 4 / 5)):
collection.update_one({"_id": author_id}, {"$set":{"Level":new_level}}, upsert=True)
await ctx.channel.send(f"{author.name} has leveled up to {new_level}!")
await bot.process_commands(ctx)
client.run("token")
如果我删除 on_message(ctx)(也是事件)中的所有内容,那么它会起作用。
我已经尝试了所有方法,但它不起作用。
例子:
await bot.process_commands(message)
这也不起作用,因为我使用的是ctx 而不是message。我觉得……
如果您对此有答案,请帮助我。
【问题讨论】:
-
它会抛出任何错误还是不起作用?
标签: python discord.py