SubredditRules
- class asyncpraw.models.reddit.rules.SubredditRules(subreddit: asyncpraw.models.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" )
- await __call__() List[asyncpraw.models.Rule]
Return a list of
Rule
s (Deprecated).- Returns:
A list of instances of
Rule
.
Deprecated since version 7.1: Use the iterator by removing the call to
SubredditRules
. For example, in order to use the iterator:subreddit = await reddit.subreddit("test") async for rule in subreddit.rules: print(rule)
- __init__(subreddit: asyncpraw.models.Subreddit)
Initialize a
SubredditRules
instance.- Parameters:
subreddit – The subreddit whose rules to work with.
- await get_rule(short_name: str | int | slice) asyncpraw.models.Rule
Return the
Rule
for the subreddit with short_nameshort_name
.- Parameters:
short_name – The short_name of the rule, or the rule number.
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:
IndexError
if a rule of a specific number does not exist.
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)
- mod() SubredditRulesModeration
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)