简体   繁体   中英

Discord Bot: This Python Code written for our discord bot to remember our book reading progress always returns zero. How can I make this work?

People in our Discord Book club, wanted our bot to remember our reading progress.

For example, the following is how I want members to log progress

Member : -update Hitchhiker's guide to the galaxy Bot : What's your progress? Member : Book 2, Chapter 20 Bot : Noted

The other day

Member : -progress (username) Bot : (username) is currently at Book 2, Chapter 20

This is the code we have so far :

import discord
import random
import os
from keep_online import keep_alive
intents = discord.Intents.default()
intents.members = True
intents.emojis=True
client=discord.Client(intents=intents)
hg2g={
  "User1":0,
  "User2":0,
  "User3":0,
  "User4":0
}
@client.event
async def on_message(message):  username=str(message.author).split("#")[0]
mess=message.content

However, this code is only returning "0" when progress is requested.

What am I doing wrong.. how do I make this work?

Please let me know if more information is needed..

Thank you so much for your help in advance..

I used commands instead of on_message as you can specify the command prefix, make an error handler etc. You have to make one more import and modify client a little bit to use commands. Also, check out MongoDB , you can make a 512 MB database for free and I think it's necessary in your case because if you save everything in variables they will disappear after you stop your code.

from discord.ext import commands

client = commands.Bot(command_prefix="-", intents=intents)
hg2g={}
another_book = {}

@client.command()
async def update(ctx, *, book_title):
    book_title = book_title.lower() # I used "lower()" to ignore if it's written in lowercase or not

    if book_title == "hitchhiker's guide to the galaxy": # you can add other books
        choosen_book = hg2g
    elif book_title == "another book": 
        choosen_book = another_book
    else:
        await ctx.send("Wait! I couldn't find this book in database!")
        return
    
    await ctx.send("What's your progress?")

    def check(m):
        return m.author == ctx.author and m.channel == ctx.channel

    msg = await client.wait_for('message', check=check)

    choosen_book.update({msg.author.id : msg.content})
    await ctx.send("Noted")


@client.command()
async def progress(ctx, user : discord.Member, *, book_title):
    book_title = book_title.lower()

    if book_title == "hitchhiker's guide to the galaxy":
        msg = hg2g.get(user.id)
    elif book_title == "another book":
        msg = another_book.get(user.id)
    else:
        await ctx.send("I couldn't find this book in database!")
        return
    
    if msg is not None:
        await ctx.send(f"{user.mention} is currently at: {msg}")
    else:
        await ctx.send(f"{user.mention} doesn't have any progress.")


@update.error
@progress.error
async def book_error(ctx, error: commands.CommandError): # if someone doesn't specify book title
    if isinstance(error, commands.MissingRequiredArgument):
        await ctx.send("Please specify book title and/or user!")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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