Set a flag on message for identification - python

I am looking for a way to set a flag or some kind of identifier on a discord.Message object so that later, for example if a reaction is added, I could identify it as a certain type and do something with it. I need something similar to discord.MessageFlags, but with the option to set the key myself, or ideally even make it of arbitrary type (not Just Boolean).
Is there anything built in that I could use, or some hacky solution?
Also, if someone asks, I don't want to set up a database.
Edit: Alternatively is there a way to see if a certain bot command have sent a particular message?

Related

How to make my Discord bot respond to both custom prefixes and mentions when I use a function to get the prefix?

I would like to preface this with saying that I asked this question previously and it got closed, marked as a duplicate. I saw those other questions before. I tried those solutions. They didn't work. They don't work with prefix functions, only prefix strings. This is why I asked the question. I have reworded a few things, added info to the title, as much as I can think of to get this question through so I can get the answer I need. Please, please do not close this one. I want an answer that works.
I have a function that retrieves a custom prefix from a database file, but I would like to implement the ability to also respond to mentions, as in the when_mentioned() function. However, I can not pass in both functions to the command_prefix parameter when I am initializing my bot, nor can I add my prefix function to the when_mentioned_or() function to combine the two. Is there any way I could go about doing this? I've been searching for answers for the last hour and have found nothing that works yet.
You can simply use the when_mentioned function in your own function that retrieves a custom prefix. You can call it with the same bot and msg that you're passed and it will return a list of the mention formats (for with and without a nickname) that you can use. You can then return that list with your custom prefix appended.
Alternatively, you can simply directly return a list with your custom prefix, bot.user.mention (see the documentation for Bot.user and ClientUser.mention), and the nickname format for that mention (<#!{user_ID}> rather than <#{user_ID}>; see the documentation for ClientUser.id).

How do you properly modify packet data in Scapy?

I’ve been using Scapy a bit and have been messing around with making packets. A problem I have is trying to specify certain data/options in packets. For example, I want to make a DHCP packet with option 60 but I don’t know what is valid input data to use for this. I know the packet should have a DHCP.options list and I can add options like this into the list, but how do I know what type and what range of data I can actually use? I also know I can type DHCPOptions at the interpreter to see that it’s a ‘vendor class id’, but how would I actually properly add it to a packet?
Another example: for a TCP timestamp option, I discovered that I have to enter that as (‘Timestamp’, (int,int)), in other words a tuple of the string Timestamp and a tuple within that tuple of two integers, within some sort of range I don’t know.
So ultimately, my question is where/how do I find out how to add valid input data into packets in Scapy? Is there a list or documentation somewhere? I’ve searched around but couldn’t find it even in the source code and the documentation doesn’t seem helpful at all. Help is greatly appreciated!
Sample code:
p = Ether()/IP()/UDP()/BOOTP()/DHCP()
# ?:what goes in the list to correctly add any option?
p[DHCP].options = [(?,?)]
# Trying to add option 60 here, unsure how
p[DHCP].options.append( ('vendor_class_id', ?) )
I use option 60 merely as an example, but I want to know how to add any valid option.
well, this might not be the full answer, since i also found it hard to find such info, but...
for some options, you can check in dhcp.py how they are defined in DHCPOptions dictionary - for example, you see that renewal_time by its definition is of int type and as default set to 21600 - IntField("renewal_time", 21600).
for other info, i suggest to dig through RFCs. for example, RFC 2132 describes DHCP Options and BOOTP Vendor Extensions.
RFC 1497 solely refers to Vendor options.

Updating files with a Perforce trigger before submit

I understand that this question has, in essence, already been asked, but that question did not have an unequivocal answer, so please bear with me.
Background: In my company, we use Perforce submission numbers as part of our versioning. Regardless of whether this is a correct method or not, that is how things are. Currently, many developers do separate submissions for code and documentation: first the code and then the documentation to update the client-facing docs with what the new version numbers should be. I would like to streamline this process.
My thoughts are as follows: create a Perforce trigger (which runs on the server side) which scans the submitted documentation files (such as .txt) for a unique term (such as #####PERFORCE##CHANGELIST##NUMBER###ROFL###LOL###WHATEVER#####) and then replaces it with the value of what the change list would be when submitted. I already know how to determine this value. What I cannot figure out, is how or where to update the files.
I have already determined that using the change-content trigger (whether possible or not), which
"fire[s] after changelist creation and file transfer, but prior to committing the submit to the database",
is the way to go. At this point the files need to exist somewhere on the server. How do I determine the (temporary?) location of these files from within, say, a Python script so that I can update or sed to replace the placeholder value with the intended value? The online documentation for Perforce which I have found so far have not been very explicit on whether this is possible or how the mechanics of a submission at this stage would work.
EDIT
Basically what I am looking for is RCS-like functionality, but without the unsightly special character sequences which accompany it. After more digging, what I am asking is the same as this question. However I believe that this must be possible, because the trigger is running on the server side and the files had already been transferred to the server. They must therefore be accessible by the script.
EXAMPLE
Consider the following snippet from a release notes document:
[#####PERFORCE##CHANGELIST##NUMBER###ROFL###LOL###WHATEVER#####] Added a cool new feature. Early retirement is in sight.
[52702] Fixed a really annoying bug. Many lives saved.
[52686] Fixed an annoying bug.
This is what the user submits. I then want the trigger to intercept this file during the submission process (as mentioned, at the change-content stage) and alter it so that what is eventually stored within Perforce looks like this:
[52738] Added a cool new feature. Early retirement is in sight.
[52702] Fixed a really annoying bug. Many lives saved.
[52686] Fixed an annoying bug.
Where 52738 is the final change list number of what the user submitted. (As mentioned, I can already determine this number, so please do dwell on this point.) I.e., what the user sees on the Perforce client console is.
Changelist 52733 renamed 52738.
Submitted change 52738.
Are you trying to replace the content of pending changelist files that were edited on a different client workspace (and different user)?
What type of information are you trying to replace in the documentation files? For example,
is it a date, username like with RCS keyword expansion? http://www.perforce.com/perforce/doc.current/manuals/p4guide/appendix.filetypes.html#DB5-18921
I want to get better clarification on what you are trying to accomplish in case there is another way to do what you want.
Depending on what you are trying to do, you may want to consider shelving ( http://www.perforce.com/perforce/doc.current/manuals/p4guide/chapter.files.html#d0e5537 )
Also, there is an existing Perforce enhancement request I can add your information to,
regarding client side triggers to modify files on the client side prior to submit. If it becomes implemented, you will be notified by email.
99w,
I have also added you to an existing enhancement request for Customizable RCS keywords, along
with the example you provided.
Short of using a post-command trigger to edit the archive content directly and then update the checksum in the database, there is currently not a way to update the file content with the custom-edited final changelist number.
One of the things I learned very early on in programming was to keep out of interrupt level as much as possible, and especially don't do stuff in interrupt that requires resources that can hang the system. I totally get that you want to resolve the internal labeling in sequence, but a better way to do it may be to just set up the edit during the trigger so that a post trigger tool can perform the file modification.
Correct me if I'm looking at this wrong, but there seems a bit of irony, or perhaps recursion, if you are trying to make a file change during the course of submitting a file change. It might be better to have a second change list that is reserved for the log. You always know where that file is, in your local file space. That said, ktext files and $ fields may be able to help.

Using column_validators in pycassa

I'm trying to setup some column_validators in pycassa but having troubles in doing so with some kind of supercolum setup: I don't seem to be able to set a validator for a column contained in a supercolumn. I'm trying something like
cf.column_validators['supercolumn_name']['column_name'] = types.FloatType()
which doesn't work because the second dictionary isn't initialized yet, trying to set it to something like
cf.column_validators['supercolumn_name'] = {}
doesn't work either... So any example on how to handle this are appreciated, as the official pycassa doc doesn't show any more detailled information on this.
Furthermore these validators don't seem to be persistent, so is there any possibility on setting them persistently?
EDIT: After looking at pycassa's source I found out, that for the validators you do not have to specify the supercolumn the column is contained in, so
cf.column_validators['column_name'] = types.FloatType()
should do the trick! Still the question remains if the validators can be made persistent somehow?
You probably don't want to be modifying column_validators directly. Those are set automatically based on the column_metadata properties of the column family. You can use the 'alter_column' command in pycassa to modify the column family schema in Cassandra. Then column_validators will be set correctly permanently.
http://pycassa.github.com/pycassa/api/pycassa/system_manager.html#pycassa.system_manager.SystemManager.alter_column

Is there a good language/syntax for field validation we can re-use?

I'm working on a web app (using Python & Bottle) and building a decorator for validating HTTP parameters sent in GET or POST. The early version takes callables, so this:
#params(user_id=int, user_name=unicode)
... ensures that user_id is an int, user_name is a string, and both fields exist.
But that's not enough. I want to be able to specify that user_name is optional, or that it must be non-empty and within 40 characters. Implementing that is easy enough, but I'm struggling with what syntax would be most elegant. It seems like that might be a problem someone's solved before, but I'm not finding an answer. Specifically I'm wondering if there's an elegant way to take parseable strings that provide the syntax. Something like:
#params(user_id='int:min1', user_name='unicode:required:max40')
I just don't want to invent a syntax if there's a good one floating around somewhere.
Anyone seen something like this? In any language..but I'm specifically valuing terseness and readability.
You could use lists.
#validate(user_id=[int, min(1)], user_name=[unicode,required,max(40)])
And each item could be a function (or class/object) that gets executed with the corresponding field as an argument. If it raises an error, it fails validation.

Categories