Jump to content

Issue with function in Discord bot (Python)

DreamCat04
Go to solution Solved by DreamCat04,

I figured out the problem: I wanted the command name to be /points, which required the funciton's name to be points. However, I already used points as a dictionary. I renamed the conflicting things so that they don't conflict anymore and it now works!

import discord
import random
from discord.ext import commands
from datetime import datetime, timedelta

TOKEN = 'my_token'

intents = discord.Intents.default()
intents.messages = True
intents.guilds = True
intents.guild_messages = True
intents.message_content = True

bot = commands.Bot(command_prefix='/', intents=intents)  # Set bot's command prefix and intents

points = {}
last_point_update = {}

@bot.event
async def on_ready():
    print(f'We have logged in as {bot.user}')

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return

    # Check if the user has sent a message in the last minute
    if message.author.id in last_point_update:
        if (datetime.now() - last_point_update[message.author.id]).total_seconds() < 60:
            # User has already received points within the last minute
            return

    # Give the user 10 points
    if message.author.id in points:
        points[message.author.id] += 10
    else:
        points[message.author.id] = 10

    # Update the last point update time for the user
    last_point_update[message.author.id] = datetime.now()

    await bot.process_commands(message)  # Required to process bot commands

@bot.command()
async def gamble(ctx, points_to_gamble: int):
    if points_to_gamble > 0:
        outcome = random.choice([True, False])
        if outcome:
            await ctx.send(f"Congratulations! You won {points_to_gamble * 2} points. You now have {points[ctx.author.id]} points")
            # Give the user the points they won
            if ctx.author.id in points:
                points[ctx.author.id] += points_to_gamble * 2
            else:
                points[ctx.author.id] = points_to_gamble * 2
        else:
            await ctx.send(f"Sorry, you lost the gamble. You now have {points[ctx.author.id]} points")
            # Deduct points from the user
            if ctx.author.id in points:
                points[ctx.author.id] -= points_to_gamble
            else:
                points[ctx.author.id] =- points_to_gamble
    else:
        await ctx.send('You have to gamble at least 1 point!')

@bot.command()
async def points(ctx):
    user_id = ctx.author.id
    if user_id in points:
        await ctx.send(f"You have {points[user_id]} points.")
    else:
        await ctx.send("You don't have any points yet.")

bot.run(TOKEN)

I'm trying to make a bot for quick gambling in Discord (with points, no money involved). I got this far but I'm getting stuck now. I get the following error: Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\client.py", line 441, in _run_event
    await coro(*args, **kwargs)
  File "c:\Users\user\Desktop\dc-bot.py", line 35, in on_message
    if message.author.id in points:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: argument of type 'Command' is not iterable

This appears when I send the command /points into my discord server to which I added this bot. What did I do wrong? ChatGPT couldn't help me resolve it unfortunately, so I'm asking you guys for some help

Link to comment
Share on other sites

Link to post
Share on other sites

I figured out the problem: I wanted the command name to be /points, which required the funciton's name to be points. However, I already used points as a dictionary. I renamed the conflicting things so that they don't conflict anymore and it now works!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×