SubredditRules¶
- class asyncpraw.models.reddit.rules.SubredditRules(subreddit)¶
Provide a set of functions to access a
Subreddit’s rules.For example, to list all the rules for a subreddit:
subreddit = await reddit.subreddit("test") async for rule in subreddit.rules: print(rule)
Moderators can also add rules to the subreddit. For example, to make a rule called
"No spam"in r/test:subreddit = await reddit.subreddit("test") await subreddit.rules.mod.add( short_name="No spam", kind="all", description="Do not spam. Spam bad" )
- Parameters:
subreddit (asyncpraw.models.Subreddit)
- __init__(subreddit)¶
Initialize a
SubredditRulesinstance.- Parameters:
subreddit (
Subreddit) – The subreddit whose rules to work with.- Return type:
None
- await get_rule(short_name, *, fetch=True)¶
Return the
Rulefor the subreddit with the short name/number/sliceshort_name.- Parameters:
short_name (
SupportsIndex) – The short name of the rule, or the rule number.fetch (
bool) – Determines if Async PRAW will fetch the object (default:True).
- Return type:
This method is to be used to fetch a specific rule, like so:
rule_name = "No spam" subreddit = await reddit.subreddit("test") rule = await subreddit.rules.get_rule(rule_name) print(rule)
You can also fetch a numbered rule of a subreddit.
Rule numbers start at
0, so the first rule is at index0, and the second rule is at index1, and so on.- Raises:
IndexErrorif a rule of a specific number does not exist.- Parameters:
short_name (SupportsIndex)
fetch (bool)
- Return type:
Note
You can use negative indexes, such as
-1, to get the last rule. You can also use slices, to get a subset of rules, such as the last three rules withget_rule(slice(-3, None)).For example, to fetch the second rule of r/test:
subreddit = await reddit.subreddit("test") rule = await subreddit.rules.get_rule(1)
To get the last three rules in a subreddit:
subreddit = await reddit.subreddit("test") rules = await subreddit.rules.get_rule(slice(-3, None)) for rule in rules: print(rule)
If you don’t need the object fetched right away (e.g., to utilize a class method) you can do:
subreddit = await reddit.subreddit("test") rule = await subreddit.rules.get_rule("No Spam", fetch=False) await rule.mod.delete()
- mod()¶
Contain methods to moderate subreddit rules as a whole.
To add rule
"No spam"to r/test try:subreddit = await reddit.subreddit("test") await subreddit.rules.mod.add( short_name="No spam", kind="all", description="Do not spam. Spam bad" )
To move the fourth rule to the first position, and then to move the prior first rule to where the third rule originally was in r/test:
subreddit = await reddit.subreddit("test") rules = [rule async for rule in subreddit.rules] new_rules = rules[3:4] + rules[1:3] + rules[0:1] + rules[4:] # Alternate: [rules[3]] + rules[1:3] + [rules[0]] + rules[4:] new_rule_list = await subreddit.rules.mod.reorder(new_rules)
- Return type: