Whenever I ran the “def Register” function I get “getting you registered message twice” and when I write to the file, it writes to it twice as well. I’m not sure what’s going on. Here is my code:
from discord.ext import commands from discord import member from discord import Intents import datetime current_time = str(datetime.datetime.now()) user_instance = None intents = discord.Intents.all() intents.message_content = True bot = commands.Bot(intents=intents, command_prefix="!", case_insensitive=True) @bot.command(name="register", description="Register a User") async def register(ctx): global user_instance try: await ctx.send("Getting you registered...") user = ctx.author user_instance = User(user) with open("user.txt", "a") as file: file.write(str(user) + "\n") except discord.HTTPException as e: await ctx.send("Oops! There was a problem Registering you. Speak with your admin.") @bot.command(name="play", description="Playing the Guessing Game") async def play(ctx, number): global user_instance print(user_instance) try: if user_instance is not None: await user_instance.play(ctx, number) else: await ctx.send("Please register first using !register.") except discord.HTTPException as e: print(f"Oops! There was a problem starting the game. Speak with your admin. Command Error: {e}") @bot.command(name="points", description="Amount of points user has") async def points(ctx): global user_instance await user_instance.point(ctx)
I ran it in a docker container but ended up with the same result.
It seems like the issue might be related to the fact that you are defining user_instance as a global variable. Since the bot might be handling multiple requests concurrently, using a global variable for user instances might lead to unexpected behavior, especially in a multi-user environment.
user_instance
To fix this issue, you should avoid using a global variable for user_instance and instead create an instance for each user separately. You can maintain a dictionary to keep track of user instances using their IDs.
Here’s a modified version of your code:
from discord.ext import commands from discord import Intents import datetime import discord intents = discord.Intents.all() intents.message_content = True bot = commands.Bot(intents=intents, command_prefix="!", case_insensitive=True) user_instances = {} class User: def __init__(self, user): self.user = user self.points = 0 async def play(self, ctx, number): # Your play logic here pass async def point(self, ctx): # Your point logic here pass @bot.command(name="register", description="Register a User") async def register(ctx): try: await ctx.send("Getting you registered...") user = ctx.author user_instances[user.id] = User(user) with open("user.txt", "a") as file: file.write(str(user) + "\n") except discord.HTTPException as e: await ctx.send("Oops! There was a problem Registering you. Speak with your admin.") @bot.command(name="play", description="Playing the Guessing Game") async def play(ctx, number): user = user_instances.get(ctx.author.id) if user is not None: await user.play(ctx, number) else: await ctx.send("Please register first using !register.") @bot.command(name="points", description="Amount of points user has") async def points(ctx): user = user_instances.get(ctx.author.id) if user is not None: await user.point(ctx) else: await ctx.send("Please register first using !register.") if __name__ == "__main__": bot.run("YOUR_BOT_TOKEN")
In this modification, each user has a separate instance of the User class, and the instances are stored in the user_instances dictionary using the user ID as the key. This should prevent the issues with multiple users and repeated messages.
User
user_instances