How do I get python to store data from multiple people?


Tristan 360

I'm trying to make a discord.py async bot that rewards people with brownie points when asked (hehe). It's supposed to be a local bot for me and my friends, but I don't want to update everyone's brownie point into the code every time I restart it. my code is here:

import command

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import random

#Here is the basic command:
#await client.send_message(message.channel, messsage)

#global hub
me = 0
discordname = "PRIVATE"

Client = discord.Client()
client = commands.Bot(command_prefix = "")

@client.event
async def on_ready():
    print ("the brownies are loaded and ready to go!")

@client.event
async def on_message(message):
    if message.content.startswith("bb! give"):
        args = message.content.split(" ")
        if args[1:][1] == "(my user ID)":
            global me
            global discordname
            me = me + int(args[1:][2])
            await client.send_message(message.channel, str(int(args[1:][2])) + "  brownie points have been given to " + str(discordname))
    if message.content.startswith("bb! brownies"):
        args = message.content.split(" ")
        if args[1:][1] == "(my user ID)":
            await client.send_message(message.channel, str(discordname) + " 
currently has " + str(me) + " brownie points.")

client.run(TOKEN)

I want the bot to create a new variable every time a user joins the server. However, every attempt I've tried (ie pickle module, shelve module...) has produced one of the following consequences:

  1. it does nothing
  2. it fails
  3. it returns an error
Patrick Howe

commandsWhy import the extension if you don't use it ? Take advantage of the commandsextension and extract your code from the on_messageevent . This lets you take advantage of some cool features of the commandsextension , like converters

from discord.ext.commands import Bot
import discord
from collections import defaultdict

bot = Bot('!')

points = defaultdict(int)

@bot.event
async def on_ready():
    print("Ready")

@bot.command(pass_context=True)
async def give(ctx, member: discord.Member):
    points[member.id] += 1
    bot.say("{} now has {} points".format(member.mention, points[member.id]))

@bot.command(pass_context=True)
async def points(ctx, member: discord.Member):
    bot.say("{} has {} points".format(member.mention, points[member.id]))

bot.run('token')

As for persisting your data, you have several options. You could write the data to a file and then read it back, but I suggest you consider setting up a database. There are many online resources elsewhere and on this site that can show you how. If you do go that route, you should probably use something like .asyncpg

Related


How do I get the URL of the Core Data persistent store to delete?

Josh Parrado I want functionality persistentStoreCoordinator.destroyPersistentStoreAtURL(...). I'm creating the container in such a way, and NSPersistentContainer(name: "MyProject")what I want to know is how to get the URL of the persistentStore used in the fi

How do I get the URL of the Core Data persistent store to delete?

Josh Parrado I want functionality persistentStoreCoordinator.destroyPersistentStoreAtURL(...). I'm creating the container in this way and NSPersistentContainer(name: "MyProject")what I want to know is how to get the URL of the persistentStore used in the first

how do i get data from the table

Ruben Can someone assist me how to get data from a table in Excel 2013? I have a table called Personal: I want to have a control in a cell like this: (I don't know how to add this control in a cell) So I can select data from the table: I googled a lot before a

how do i get data from the table

Ruben Can someone assist me how to get data from a table in Excel 2013? I have a table called Personal: I want to have a control in a cell like this: (I don't know how to add this control in a cell) So I can select data from the table: I googled a lot before a

Q: How do I exclude people's names from the table

Gad_Rachmany I'm trying to use bigquery to find the 10 most mentioned people on 4 Israeli news sites using the gdeltv2 dataset. I managed to get the 10 most mentioned people and now I want to exclude two people from the top 10 list, "Maccabi Haifa" and "Reuben

I want to get numeric data from string. How to do this in python?

Shantanu Agarwal I want to get numeric data from string. How to do this in python? ? ? For example, extract 23 and 45 from the string "data23/45 data" Joachim Isaksson I'm sure there are hundreds of ways, but one way is to use a regular expression to split the

I want to get numeric data from string. How to do this in python?

Shantanu Agarwal I want to get numeric data from string. How to do this in python? ? ? For example, extract 23 and 45 from the string "data23/45 data" Joachim Isaksson I'm sure there are hundreds of ways, but one way is to use regex to split the string into gr

I want to get numeric data from string. How to do that in python?

Sshantanu Agarwal I want to get numeric data from string. How to do that in python??? For e.g. From string "data23/45 data" extract 23 and 45 Joachim Isaksson I'm sure there are hundreds of ways, but one way is to use a regular expression to split the string i

MySQL: How do I get 3 columns from multiple tables?

username I have 2 tables: forum: +-----------+----------------+-----------------+-------------+---------------+ | forum_id |user_profile_id |profile_forum_id |profile_role |profile_status | +-----------+----------------+-----------------+-------------+-------

MySQL: How do I get 3 columns from multiple tables?

username I have 2 tables: forum: +-----------+----------------+-----------------+-------------+---------------+ | forum_id |user_profile_id |profile_forum_id |profile_role |profile_status | +-----------+----------------+-----------------+-------------+-------

How would I get multiple data from shared preferences?

Monil Tuhin I have multiple data saved using shared preferences. I want to read multiple data from shared preferences. I tried but couldn't. I can read one data but not multiple data. thanks. //Save multiple data private static int incrementedValue = 0; sav

How do I store encrypted data

Timek Sala: I'm trying to store and retrieve my encrypted text data in UserDefaults, but I'm having trouble retrieving it. I use CryptoKit to encrypt and decrypt data. The error message is: "Cannot cast value of type 'Foundation.__NSSwiftData' (0x7fff89d1ee50)

How do I store encrypted data

Timek Sala: I'm trying to store and retrieve my encrypted text data in UserDefaults, but I'm having trouble retrieving it. I use CryptoKit to encrypt and decrypt data. The error message is: "Cannot cast value of type 'Foundation.__NSSwiftData' (0x7fff89d1ee50)

How do I get python to add and divide multiple variables?

unknown Greetings Python programmers. I'm building a raspberry pi project that aims to use a humidity sensor in my garden. My program is designed to take 30 seconds of readings and then average them into a single number. It uses four sensors and runs python3.5

How to get data from webhook to store in database?

Judo 1992 I am using a Webhook based on SendGrid API v3. The webhook is fully set up as the endpoint that SendGrid publishes to, but I need to be able to receive that parsed data and store it in a database using PHP, but don't know how. I have used the API bef

How do I get 4 bits from a 16 bit data?

Sajal Singh: I use the getShort() method of the byte buffer to get my short value, I convert it to an int and print the binary value, which is a 16-bit data and I need to access from that 16-bit data 4-bit int value. short bitRateShort = buf.getShort(32 + 2);