I have a command where you can enter a correct answer. If it is correct, the user is credited with points in a JSON. But my update function seems to be broken, because after another correct execution an entry is made again in the JSON for the same user. However, I just want the points to update. Also, the JSON stops after the second entry about the user. What was wrong?
Code:
correct_answers = "A"
# Open the JSON after start
def json_open():
with open('users.json', 'r', encoding='utf-8') as f:
users = json.load(f)
return users
class Questions(commands.Cog, name='Question'):
"""Question bot"""
def __init__(self, bot):
super().__init__()
self.bot = bot
#commands.command()
async def question(self, ctx, answer):
self.question.enabled = False
global correct_answers
if correct_answers != answer:
await ctx.author.send(f"You guessed {answer} which is **wrong**. Good luck next time!")
await ctx.message.delete()
return
# OPEN JSON FILE, LOAD DATA
with open('users.json', 'r') as f:
users = json.load(f)
await self.update_data(users, ctx.message.author)
await self.add_experience(users, ctx.message.author, 10)
with open('users.json', 'w') as f:
json.dump(users, f)
await ctx.message.delete()
# UPDATE DATA
async def update_data(self, users, user):
if not user.id in users:
users[user.id] = {}
users[user.id]['Points'] = 0
#users[user.id]['level'] = 1
async def add_experience(self, users, user, exp):
users[user.id]['Points'] += exp
It looks like the last functions do not work or is the add_experience function not needed?
The JSON looks like this after the second execution:
{"MYID": {"Points": 10}, "MYIDAGAIN": {"Points": 10}}
Somehow it is converted into a str so you have to update the function a bit. To explain it better:
Turn the user.id into a str.
async def update_data(self, users, user):
key = str(user.id)
if key not in users:
users[key] = {}
users[key]['Points'] = 0
async def add_experience(self, users, user, exp):
users[str(user.id)]['Points'] += exp
Maybe also have a look at the page where the problem is explained.
I think that you have to string the user ID like this
users[str(user.id)]['Points'] += exp
Related
I am trying to make a twitter points program. Basically, you get points based off of the number of likes, retweets and replies your post with a specified hashtag gets. I made an API to get these points from a database but fastAPI is not doing all the funtions specified to return the correct values.
API code:
DATABASE_URL = "mysql+mysqlconnector://root:password#localhost:3306/twitterdb"
database = Database(DATABASE_URL)
metadata_obj = MetaData()
engine = create_engine(
DATABASE_URL, connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
metadata = sqlalchemy.MetaData()
Base = declarative_base()
user_points = sqlalchemy.Table(
"points",
metadata_obj,
sqlalchemy.Column("username", sqlalchemy.String,),
sqlalchemy.Column("rt_points", sqlalchemy.Integer,),
sqlalchemy.Column("reply_points", sqlalchemy.Integer),
sqlalchemy.Column("like_points", sqlalchemy.Integer),
sqlalchemy.Column("total_points", sqlalchemy.Integer)
)
engine = sqlalchemy.create_engine(
DATABASE_URL
)
metadata.create_all(engine)
app = FastAPI()
#app.on_event("startup")
async def connect():
await database.connect()
#app.on_event("shutdown")
async def shutdown():
await database.disconnect()
class UserName(BaseModel):
rt_points: int
reply_points: int
like_points: int
total_points : int
#app.get('/userdata/', response_model=UserName)
async def get_points(user: str):
username=user
metrics.clear()
tweets_list = tweet_id(username)
tweets_list.get_tweet_ids(str(username))
metrics.main()
summing=summer(username)
summing.sum_fun(str(username))
query = user_points.select().where(user_points.c.username == username)
user = await database.fetch_one(query)
return {**user}
if __name__ == "__main__":
uvicorn.run("main:app", reload= True, host="127.0.0.1", port=5000, log_level="info")
code for metrics.py:
ids=[]
class tweet_id:
def __init__(self, name):
self.name = name
def get_tweet_ids(self, name):
try:
connection = mysql.connector.connect(host='localhost',
database='twitterdb',
user='root',
password='password')
cursor = connection.cursor()
query="truncate table twitterdb.points"
query1="truncate table twitterdb.Metrics"
sql_select_query = """SELECT tweetid FROM twitterdb.StreamData WHERE username = %s"""
# set variable in query
cursor.execute(query)
cursor.execute(query1)
cursor.execute(sql_select_query, (name,))
# fetch result
record = cursor.fetchall()
for row in record:
ids.append(int(row[0]))
except mysql.connector.Error as error:
print("Failed to get record from MySQL table: {}".format(error))
finally:
if connection.is_connected():
cursor.close()
connection.close()
def create_url():
tweet_fields = "tweet.fields=public_metrics"
converted_list = [str(element) for element in ids]
id_list = ",".join(converted_list)
url = "https://api.twitter.com/2/tweets?ids={}&{}".format(id_list, tweet_fields)
return url
#curl 'https://api.twitter.com/2/tweets?ids=1459764778088337413&tweet.fields=public_metrics&expansions=attachments.media_keys&media.fields=public_metrics' --header 'Authorization: Bearer $Bearer
def bearer_oauth(r):
"""
Method required by bearer token authentication.
"""
r.headers["Authorization"] = f"Bearer {bearer_token}"
return r
def connect_to_endpoint(url):
response = requests.request("GET", url, auth=bearer_oauth)
print(response.status_code)
if response.status_code != 200:
raise Exception(
"Request returned an error: {} {} {}".format(
response.status_code, response.text, ids
)
)
return url
return response.json()
def main():
def append_to_database(json_response):
#Loop through each tweet
for tweet in json_response['data']:
# Tweet ID
tweetid = tweet['id']
# Tweet metrics
retweet_count = tweet['public_metrics']['retweet_count']
reply_count = tweet['public_metrics']['reply_count']
like_count = tweet['public_metrics']['like_count']
quote_count = tweet['public_metrics']['quote_count']
connect(tweetid, retweet_count, reply_count, like_count, quote_count)
def connect(tweetid, retweet_count, reply_count, like_count, quote_count):
"""
connect to MySQL database and insert twitter data
"""
try:
con = mysql.connector.connect(host='localhost',
database='twitterdb', user='root', password='passsword', charset='utf8')
if con.is_connected():
"""
Insert twitter data
"""
cursor = con.cursor(buffered=True)
# twitter, golf
delete_previous_data_query = "truncate table Metrics"
query = "INSERT INTO Metrics (tweetid,retweet_count,reply_count,like_count,quote_count) VALUES (%s, %s, %s, %s, %s)"
cursor.execute(delete_previous_data_query)
cursor.execute(query, (tweetid,retweet_count,reply_count,like_count,quote_count))
con.commit()
except Error as e:
print(e)
cursor.close()
con.close()
return
url = create_url()
json_response = connect_to_endpoint(url)
append_to_database(json_response)
#Function to calculate sum of points and display it
class summer:
def __init__(self, name):
self.name = name
def sum_fun(self, name):
try:
con = mysql.connector.connect(host='localhost',
database='twitterdb', user='root', password='password', charset='utf8')
if con.is_connected():
cursor = con.cursor(buffered=True)
def create_points_table():
query= ("INSERT INTO twitterdb.points(username, rt_points,reply_points,like_points,total_points) (SELECT %s, SUM(quote_count + retweet_count) * 150, SUM(reply_count) * 50, SUM(like_count) * 10, SUM(quote_count + retweet_count) * 150 + SUM(reply_count) * 50 + SUM(like_count) * 10 FROM twitterdb.Metrics)")
cursor.execute(query, (name,))
con.commit()
create_points_table();
except Error as e:
print(e)
cursor.close()
con.close()
def clear():
"""
connect to MySQL database and insert twitter data
"""
try:
con = mysql.connector.connect(host='localhost',
database='twitterdb', user='root', password='password', charset='utf8')
if con.is_connected():
cursor = con.cursor(buffered=True)
clear_points = ("truncate table twitterdb.points")
cursor.execute(clear_points)
except Error as e:
print(e)
cursor.close()
con.close()
return
What happens here is that there's a database named twitterdb with the tables StreamData, metrics, and points.
StreamData containts tweetids and usernames of the posts that were tweeted with the specified hashtag and it is build with the Streaming API.
Here the issues is that, suppose I have the following usernames mark and ramon in the streamdata table. So when I input the username via the API as mark no issues happen, it returns the correct points for mark, but if I then enter something like mark1 or any random value, it returns the points for mark again. But then if I enter ramon it gives the right points for ramon but then if I enter the random values again, I get the same points for ramon.
Furthermore, the first time when we start the API and if we enter a random value, it returns an error that is specified in the exception as defined in connect_to_endpoint function.
The code logic here is that,
We enter a username via the API, and the get_tweet_ids function looks for that username in the streamdata table and selects all the tweet ids corresponding to that username and saves it to a list, ids. This list of ids is given to the twitter metrics API endpoint and the required values from the response is saved to the table metrics.
Then, the sum_fun is called to select the sum of values of likes, rts and replies from the metrics table, multiply it with the specified points and save it to the table points along with the username.
The API at last returns the values in the table points matching the username.
How can I get it to stop returning the values for random data? If an invalid data is given, it must raise the exception in connect_to_endpoint function, but it just returns whatever value is in the table points previously.
I tried multiple approaches to this like, clearing the values of points before all other functions and checking to return only the values corresponding to the username in the points table. But neither of them worked. When the username was checked in the points table after running it with random values, it contained the random value but with the points of the previous valid username.
NOTE: The table points is a temporary table and values are assigned only when an API call is made.
I am a complete beginner to all this and this is more of a pet project I have been working on, so please help out. Any and all help and guidance regarding my logic and design and a fix for this will be of much use. Thanks.
if the code that you have provided for metrics.py is correct your problem should comme from how you declare the variable ids.
in your code you have declare it as a global so it will not be reset at every function call or class instance creation.
what you should to is declare it in get_tweet_ids()
class tweet_id:
def __init__(self, name):
self.name = name
def get_tweet_ids(self, name):
ids=[] # modification here
try:
connection = mysql.connector.connect(host='localhost',
database='twitterdb',
user='root',
password='password')
cursor = connection.cursor()
query="truncate table twitterdb.points"
query1="truncate table twitterdb.Metrics"
sql_select_query = """SELECT tweetid FROM twitterdb.StreamData WHERE username = %s"""
# set variable in query
cursor.execute(query)
cursor.execute(query1)
cursor.execute(sql_select_query, (name,))
# fetch result
record = cursor.fetchall()
for row in record:
ids.append(int(row[0]))
return ids # modification here
except mysql.connector.Error as error:
print("Failed to get record from MySQL table: {}".format(error))
finally:
if connection.is_connected():
cursor.close()
connection.close()
with this you will have a new instance of ids at every get_tweet_ids call.
You will have to change the rest of your code according to this return statement
I'm trying to make 2 commands, One to enable commands and one to disable commands per guild, but I have problems as I don't have much experience with these json files,im trying to make it so when i disable a command it will check if the id is there and if the command is added already,and a command to enable the commmand back,where it checks for the guild id and command name
here is the code:
#client.command()
async def disablecommand(ctx, commandname):
command = client.get_command(commandname)
with open("Disabled_commands.json") as f:
configData = json.load(f)
disabledcommands = configData[f"{ctx.guild.id}"]
if str(ctx.guild.id) in configData: #check if the guild id is in list
disabledcommands.append(command)
if command in disabledcommands: #check if command is already disabled
await ctx.reply("command already disabled")
else: #if command is not disable
disabledcommands.append(command)
with open(f"{ctx.guild.id}", "r+") as f:
data = json.load(f)
data["Disabled_commands"] = disabledcommands
f.seek(0)
f.write(json.dumps(data))
f.truncate()
await ctx.send("command has been disabled")
else: #if guild id is not in json file
disabledcommands.append(command)
with open(f"{ctx.guild.id}", "r+") as f:
data = json.load(f)
data[f"{ctx.guild.id}"] = {}
data["Disabled_commands"] = disabledcommands
f.seek(0)
f.write(json.dumps(data))
f.truncate()
await ctx.send("command has been disabled")
#client.command()
async def enablecommand(ctx, commandname):
command = client.get_command(commandname)
with open("Disabled_commands.json") as f:
configData = json.load(f)
disabledcommands = configData[f"{ctx.guild.id}"]
if str(ctx.guild.id) in configData: #check if server is in the list
if command in disabledcommands: #check if command is disabled
disabledcommands.remove(command)
with open("Disabled_commands.json", "r+") as f:
data = json.load(f)
data[f"{ctx.guild.id}"] = disabledcommands
f.seek(0)
f.write(json.dumps(data))
f.truncate()
await ctx.send("Command is now enabled")
else: #if command is enabled already
await ctx.send("Command isnt disabled")
else: #if server isnt in the list
await ctx.send("Command isnt disabled")
my json should look like this:
{"816726041673990214":{"ping","mute","ban"},"786631236838883400":{"troll","thing","ping"}}
The problem is, that your json is invalid, and so the json decoder raises an error. The problem with your json is that you use {} around "ping","mute","ban", but "ping","mute","ban" is a list, so you would have to use [] around it. Your fixed json should look like this:
{"816726041673990214":["ping","mute","ban"],"786631236838883400":["troll","thing","ping"]}
I am trying to read a channel ID from a JSON. With this you should be able to determine a channel, a kind of log, where the bot messages should be sent. However, I unfortunately have no idea how to get this ID for a single guild from the JSON.
My approaches:
async def logchannel():
with open("src/logchannel.json", "r") as f:
lchannel = json.load(f)
return lchannel
(Says at the top of the class)
#commands.command(hidden=True)
#commands.guild_only()
#commands.has_permissions(manage_messages=True)
async def setlog(self, ctx, channel: str):
"""Changes the log channel"""
with open('src/logchannel.json', 'r', encoding='utf-8') as fp:
log_channel = json.load(fp)
try:
log_channel[f"{ctx.channel.id}"] = channel
except KeyError:
new = {ctx.channel.id: channel}
log_channel.update(new)
await ctx.send(f "Channel set to: `{channel}`")
with open('src/logchannel.json', 'w', encoding='utf-8') as fpp:
json.dump(log_channel, fpp, indent=2)
Should then be the specified channel/always update itself.
#commands.command()
async def cdel(self, ctx, channel: discord.TextChannel):
with open('src/logchannel.json', 'r', encoding='utf-8') as fp:
log_channel = json.load(fp)
await channel.delete()
await log_channel.send(f "**Successfully deleted channel `{channel}`!**")
Which gives me the obvious error AttributeError: 'dict' object has no attribute 'send'. I probably have the error there, but don't see it/it doesn't work at all the way I want.
To put it in a nutshell: I want the user to be able to choose his log-channel the bot sends like all the ban messages etc. The user itself can always change the channel by command. The only problem I now have is that the bot gives me the error above as I am not requestion the channel in the right way out of the JSON.
EDIT:
This is how my JSON file looks like:
{
"811573570831384638": "811578547751616532",
"811623743959990295": "811573570831384638"
}
First number is the channel ID the command was executed in and the second key is the defined mod-log channel.
json.load(fp) gets you the whole json file as dictionary. You should get the channel id from it.
#commands.command(hidden=True)
#commands.guild_only()
#commands.has_permissions(manage_messages=True)
async def setlog(self, ctx, channel: discord.TextChannel):
"""Changes the log channel"""
with open('src/logchannel.json', 'r', encoding='utf-8') as fp:
log_channel = json.load(fp)
try:
log_channel[str(ctx.guild.id)] = channel.id
except KeyError:
new = {str(ctx.guild.id): channel.id}
log_channel.update(new)
await ctx.send(f"Channel set to: `{channel}`")
with open('src/logchannel.json', 'w', encoding='utf-8') as fpp:
json.dump(log_channel, fpp, indent=2)
With a little bit edit in your code, you can use this command to change log channel. You just have to mention the channel you want to make the new log channel. It will save the id of the guild where the command is used and the mentioned channel as key and value.
When you want to send a message to this channel, you have to use guild.get_channel() or discord.utils.get(). Because you need a discord.TextChannel instance in order to send message to this channel.
#commands.command()
async def cdel(self, ctx, channel: discord.TextChannel):
with open('src/logchannel.json', 'r', encoding='utf-8') as fp:
log_channel = json.load(fp)
await channel.delete()
log_c = ctx.guild.get_channel(log_channel[str(ctx.guild.id)])
# Or you can use:
# log_c = discord.utils.get(ctx.guild.text_channels, id=log_channel[str(ctx.guild.id)])
await log_c.send(f"**Successfully deleted channel `{channel}`!**")
If you want to update the log channel ID when the current log channel is deleted, you can use the on_guild_channel_delete event to check it.
#client.event
async def on_guild_channel_delete(channel):
with open('src/logchannel.json', 'r', encoding='utf-8') as fp:
log_channel = json.load(fp)
if channel.id in log_channel.values():
values = list(log_channel.values())
keys = list(log_channel.keys())
log_channel[keys[values.index(channel.id)]] = <channel id>
with open('src/logchannel.json', 'w', encoding='utf-8') as fpp:
json.dump(log_channel, fpp, indent=2)
You have to put a backup channel id between <channel id> in case of you delete the log channel. So if you delete the log channel, it will automatically changes it with this backup id.
I am using Django to display rows in mysql.
The table in mysql has a primary key which it bigint, and one of them is 871195445245063168, 18 digits.
But on my page, I see 871195445245063200 displayed, the least 3 digits are rounded. I am wondering where I make it wrong.
1, I define a class with a function named data_query to query mysql.
class MyQuery:
self.conn = MySQLdb.connect(host = self.DBHOST, user = self.DBUSER,
passwd = self.DBPWD,port = self.DBPORT,charset = self.CHARSET,connect_timeout=3)
def data_query(self,sql):
cursor = self.conn.cursor(MySQLdb.cursors.DictCursor)
start = time.time()
cursor.execute(sql)
end = time.time()
sql_time = end - start
column_description = cursor.description
column_name = [ column[0] for column in column_description ]
res = cursor.fetchall()
cursor.close()
self.conn.close()
return res,column_name,sql_time
2, I defined a json encoder as follows
class CJsonEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.datetime):
try:
return obj.strftime('%Y-%m-%d %H:%M:%S')
except ValueError:
return str(obj)
elif isinstance(obj, datetime.date):
try:
return obj.strftime('%Y-%m-%d')
except ValueError:
return str(obj)
elif isinstance(obj,datetime.timedelta):
return str(obj)
elif isinstance(obj, decimal.Decimal):
return float(obj)
elif isinstance(obj,ObjectId):
return str(obj)
else:
return json.JSONEncoder.default(self, obj)
3, I get my display like this, with sensitive info replaced.
db = MyQuery(host, user, pwd, port)
sql_statement = 'select * from mytable where Findex=871195445245063168 limit 10'
sql_result, table_column_name, sql_time = db.data_query(sql_statement)
query_result = {}
column_name = column_format(table_column_name)
query_result['column'] = column_name
query_result['data'] = list(sql_result)
return HttpResponse(json.dumps(query_result, cls=CJsonEncoder), content_type='application/json')
So, what I go wrong here? Thanks.
This is a JavaScript issue. Your number is bigger than the largest safe integer in JavaScript (Number.MAX_SAFE_INTEGER), so it is rounded.
You can verify this in your browser console or in node.js
$ node
> x = 871195445245063168
871195445245063200
I assume you are either using your response in some kind of JavaScript frontend or you have some Browser extension to render the JSON, which is written in JavaScript.
If you request that URL with a client like curl, you will see that it is returned correctly from the server.
i have two tables, one is MeasuredController and MeasuredGrid, and there is no relation like foreign key or manytoman etc , and in admin i have to show two fields of MeasuredGrid i.e power and status, where MeasuredController's senddate = MeasuredGrid's senddate, in two different column, i have wrote code like below, but in the current code , the database will be hit two time for each object, so is there a way like select related or use cache concept ?
list_display = ("grid_status", "grid_power")
def grid_status(self, obj):
STATUS_CHOICES = {0:"Outage", 1:"No Outage" }
mobj = MeasuredGrid.objects.filter(senddate=obj.senddate).latest("senddate")
try:
return STATUS_CHOICES[int(mobj.status)], 2
except:
pass
grid_status.short_description = 'Grid Status'
def grid_power(self, obj):
mobj = MeasuredGrid.objects.filter(senddate=obj.senddate).latest("senddate")
return mobj.power
grid_power.short_description = 'Grid Power[W]'
You can use Cache Framework. It is very easy:
# coding: utf-8
from django.core.cache import cache
from django.contrib import admin
from .models import MeasuredController, MeasuredGrid
class MeasuredControllerAdmin(admin.ModelAdmin):
list_display = ("grid_status", "grid_power")
STATUS_CHOICES = {
0: "Outage",
1: "No Outage",
}
def grid_status(self, obj):
mobj = self._get_mobj_data(obj)
return mobj['status']
grid_status.short_description = 'Grid Status'
def grid_power(self, obj):
mobj = self._get_mobj_data(obj)
return mobj['power']
grid_power.short_description = 'Grid Power[W]'
def _get_mobj_data(self, obj):
"""Get a relevant MeasuredGrid object for a given MeasuredController"""
data = cache.get('mobj_%s' % obj.pk)
if data is not None:
return data
mobj = MeasuredGrid.objects.filter(senddate=obj.senddate).latest("senddate")
status = None
try:
status = self.STATUS_CHOICES[int(mobj.status)], 2
except: # <---------- Not the best decision. You probably need ValueError or KeyError
pass
data = {
"id": mobj.pk,
"power": mobj.power,
"status": status,
}
cache.set('mobj_%s' % obj.pk, data) # the default timeout is 300 seconds
return data
NB: the default cache backend is django.core.cache.backends.locmem.LocMemCache, so the cache will work even in dev environment (ie. DEBUG = True).